- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的程序:输入 5 个答案,然后显示包含问题编号、动物、预期答案、提交的答案和结果的表格:
插入 5 个答案后我的表格看起来如何:
问题:
如何使数据每 5 行缩进一次?另外,如何使动物不重复(即如何使从数组中选取动物的 rng 不重复,以便同一动物不会显示两次)
HTML:
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<style>
#result {
display: none;
}
</style>
<title>Animal Farm</title>
</head>
<body>
<div class='container'>
<h1>Animal Farm</h1>
<h2>Question <span id='question-number'>1</span></h2>
What does the <span id="animal-name">?</span> say?
<select id='user-selection'></select>
<button onclick='recordAnswer()'>Submit</button>
<div id='result'>
<table class='table'>
<thead>
<tr><th>Question</th><th>Animal</th><th>Expected</th><th>You Picked</th><th>Result</th></tr>
</thead>
<tbody id='result-table'>
</tbody>
</table>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="animal.js"></script>
</body>
</html>
JavaScript:
"use strict"
let farm = [] // list of all animals
let currentAnimal = undefined
let theQnum = document.getElementById("question-number")
let questionNumber = 2
let stopCounter = 1
let qArr = []
let animalArr = []
let expectedArr = []
let suppliedAnswers = []
let resultArr = []
let theAnimal
farm = generateAnimalArray()
let theMin = 0
let theMax = farm.length
let theRandom = getRandomInt(theMin, theMax)
let currentSound = farm[theRandom].says
let count = 0
let dropdown = document.getElementById("user-selection")
farm.forEach(x => {
dropdown.innerHTML = `` + dropdown.innerHTML + `` + `<option>${farm[count].says}</option>`
count++
});
let ddVal
showQuestion()
// return a random number in the range [min,max]
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
// return an animal object
function createAnimal(name, speak) {
return {
type: name,
says: speak
}
}
// generate and return a list of animals in our program
// it is expected that the return value be stored into the "farm" variable
function generateAnimalArray() {
let result = []
result.push(createAnimal('dog', 'woof'))
result.push(createAnimal('cat', 'meow'))
result.push(createAnimal('bird', 'tweet'))
result.push(createAnimal('mouse', 'squeek'))
result.push(createAnimal('cow', 'moo'))
result.push(createAnimal('duck', 'quack'))
result.push(createAnimal('fox', 'ring-ding-ding-ding-dingeringeding'))
return result
}
// Pick a random animal from the list, show it to the user and set the question number
function showQuestion() {
theAnimal = document.getElementById("animal-name")
theAnimal.innerHTML = farm[theRandom].type
}
// Get the answer, create an object with the question number, the animal type, the expected answer and the
// supplied answer and store it into the suppliedAnswer array.
//
// If we have seen 5 questions then show the result, otherwise show the next questions
function recordAnswer() {
ddVal = dropdown.value
qArr.push(questionNumber - 1)
animalArr.push(farm[theRandom].type)
expectedArr.push(currentSound)
suppliedAnswers.push(ddVal)
console.log(ddVal)
if (currentSound != ddVal) {
resultArr.push("Wrong")
}
else {
resultArr.push("Correct!")
}
stopCounter++
if (stopCounter > 5) {
window.recordAnswer = function () { return false; };
console.log(qArr)
console.log(animalArr)
console.log(expectedArr)
console.log(suppliedAnswers)
console.log(resultArr)
displayResult()
}
else {
theRandom = getRandomInt(theMin, theMax)
theAnimal.innerHTML = farm[theRandom].type
theQnum.innerHTML = questionNumber++
ddVal = dropdown.value
currentSound = farm[theRandom].says
}
}
// Show the values in the suppliedAnswers as a table
function displayResult() {
let tableDisplay = document.getElementById("result-table")
let theResult = document.getElementById("result")
theResult.style.display = "block"
qArr.forEach(x => {
tableDisplay.innerHTML = tableDisplay.innerHTML + `<td>` + x + `</td>`
})
animalArr.forEach(x => {
tableDisplay.innerHTML = tableDisplay.innerHTML + `<td>` + x + `</td>`
})
expectedArr.forEach(x => {
tableDisplay.innerHTML = tableDisplay.innerHTML + `<td>` + x + `</td>`
})
suppliedAnswers.forEach(x => {
tableDisplay.innerHTML = tableDisplay.innerHTML + `<td>` + x + `</td>`
})
resultArr.forEach(x => {
tableDisplay.innerHTML = tableDisplay.innerHTML + `<td>` + x + `</td>`
})
}
// 1. generate the farm full of animals
// 2. Create the options for the select
// 3. show the first question
document.addEventListener('DOMContentLoaded', () => {
let buffer = []
let strBuff
document.addEventListener('keydown', event => {
const charList = 'abcdefghijklmnopqrstuvwxyz0123456789';
const key = event.key.toLowerCase()
if (charList.indexOf(key) === -1) return;
buffer.push(key)
strBuff = buffer.toString()
if (strBuff == "h,e,l,p") {
window.open('https://www.youtube.com/watch?v=xvypmult19M', '_blank')
buffer = []
strBuff
}
})
})
最佳答案
forEach
提供第二个参数,为您提供当前迭代。因此,要每五个项目缩进一次,您可以这样做:
arr.forEach((x, i) => {
if(i % 5 == 0) tableDisplay.innerHTML += "<td style='padding-left:20px;'>" + x + "</td>";
else tableDisplay.innerHTML += "<td>" + x + "</td>";
});
为了防止从数组中选取项目时出现重复,您应该对数组进行打乱,然后 pop()
如果您用完值,请重新填充它。这是一个例子:
var arr = [1, 2, 3, 4, 5];
var shuffledArr = randoSequence(arr);
for(var i = 0; i < 10; i++){
if(shuffledArr.length == 0) shuffledArr = randoSequence(arr);
console.log(shuffledArr.pop().value);
}
<script src="https://randojs.com/1.0.0.js"></script>
此示例使用 randojs.com 对数组进行打乱顺序为了简化随机性并使其更具可读性,因此如果您想使用此代码,请确保它位于 html 文档的 head 标记的顶部:
<script src="https://randojs.com/1.0.0.js"></script>
关于javascript - 将数组元素插入表 JavaScript/并防止随机数重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60019046/
我正在尝试创建一个包含 int[][] 项的数组 即 int version0Indexes[][4] = { {1,2,3,4}, {5,6,7,8} }; int version1Indexes[
我有一个整数数组: private int array[]; 如果我还有一个名为 add 的方法,那么以下有什么区别: public void add(int value) { array[va
当您尝试在 JavaScript 中将一个数组添加到另一个数组时,它会将其转换为一个字符串。通常,当以另一种语言执行此操作时,列表会合并。 JavaScript [1, 2] + [3, 4] = "
根据我正在阅读的教程,如果您想创建一个包含 5 列和 3 行的表格来表示这样的数据... 45 4 34 99 56 3 23 99 43 2 1 1 0 43 67 ...它说你可以使用下
我通常使用 python 编写脚本/程序,但最近开始使用 JavaScript 进行编程,并且在使用数组时遇到了一些问题。 在 python 中,当我创建一个数组并使用 for x in y 时,我得
我有一个这样的数组: temp = [ 'data1', ['data1_a','data1_b'], ['data2_a','data2_b','data2_c'] ]; // 我想使用 toStr
rent_property (table name) id fullName propertyName 1 A House Name1 2 B
这个问题在这里已经有了答案: 关闭13年前。 Possible Duplicate: In C arrays why is this true? a[5] == 5[a] array[index] 和
使用 Excel 2013。经过多年的寻找和适应,我的第一篇文章。 我正在尝试将当前 App 用户(即“John Smith”)与他的电子邮件地址“jsmith@work.com”进行匹配。 使用两个
当仅在一个边距上操作时,apply 似乎不会重新组装 3D 数组。考虑: arr 1),但对我来说仍然很奇怪,如果一个函数返回一个具有尺寸的对象,那么它们基本上会被忽略。 最佳答案 这是一个不太理
我有一个包含 GPS 坐标的 MySQL 数据库。这是我检索坐标的部分 PHP 代码; $sql = "SELECT lat, lon FROM gps_data"; $stmt=$db->query
我需要找到一种方法来执行这个操作,我有一个形状数组 [批量大小, 150, 1] 代表 batch_size 整数序列,每个序列有 150 个元素长,但在每个序列中都有很多添加的零,以使所有序列具有相
我必须通过 url 中的 json 获取文本。 层次结构如下: 对象>数组>对象>数组>对象。 我想用这段代码获取文本。但是我收到错误 :org.json.JSONException: No valu
enter code here- (void)viewDidLoad { NSMutableArray *imageViewArray= [[NSMutableArray alloc] init];
知道如何对二维字符串数组执行修剪操作,例如使用 Java 流 API 进行 3x3 并将其收集回相同维度的 3x3 数组? 重点是避免使用显式的 for 循环。 当前的解决方案只是简单地执行一个 fo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有来自 ASP.NET Web 服务的以下 XML 输出: 1710 1711 1712 1713
如果我有一个对象todo作为您状态的一部分,并且该对象包含数组列表,则列表内部有对象,在这些对象内部还有另一个数组listItems。如何更新数组 listItems 中 id 为“poi098”的对
我想将最大长度为 8 的 bool 数组打包成一个字节,通过网络发送它,然后将其解压回 bool 数组。已经在这里尝试了一些解决方案,但没有用。我正在使用单声道。 我制作了 BitArray,然后尝试
我们的数据库中有这个字段指示一周中的每一天的真/假标志,如下所示:'1111110' 我需要将此值转换为 boolean 数组。 为此,我编写了以下代码: char[] freqs = weekday
我是一名优秀的程序员,十分优秀!