我想做一个每次问题随机化的测验。我得到的东西可以随机化答案,但不能随机化问题。这是我到目前为止所得到的。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial- scale=1">
<title>Made with Thimble</title>
<link rel="stylesheet" href="style.css">
<p>
</p>
<script>
if (!("scramble" in Array.prototype)) {
Object.defineProperty(Array.prototype, "scramble", {
enumerable: false,
value: function() {
var o, i, ln = this.length;
while (ln--) {
i = Math.random() * (ln + 1) | 0;
o = this[ln];
this[ln] = this[i];
this[i] = o;
}
return this;
}
});
}
var quiz = [{
"question": ["What is the full form of IP?","hi"],
"choices": ["Internet Provider", "Internet Port", "Internet Protocol" , "Other"],
"correct": "Other"
}, {
"question": "Who is the founder of Microsoft?",
"choices": ["Bill Gates", "Steve Jobs", "Steve Wozniak" , "Martin Shaba"],
"correct": "Bill Gates"
}, {
"question": "What was your first dream?",
"choices": ["8 bits", "64 bits", "1024 bits"],
"correct": "8 bits"
}, {
"question": "The C programming language was developed by?",
"choices": ["Brendan Eich", "Dennis Ritchie", "Guido van Rossum"],
"correct": "Dennis Ritchie"
}, {
"question": "What does CC mean in emails?",
"choices": ["Carbon Copy", "Creative Commons", "other"],
"correct": "Carbon Copy"
}];
quiz.forEach(q => q.choices.scramble());
document.write(quiz[prompt("Which one?")].choices);
</script>
最佳答案
您只能提示一次,然后存储在变量中以选择所有测验元素..
quiz.forEach(q => q.choices.scramble());
var x = prompt("Select question number #:");
var ans = ""
function myFunction(item, index) {
ans += "\n[" + (index+1) + "]: " + item ;
}
quiz[x].choices.forEach(myFunction);
var y = prompt(quiz[x].question+"\nYour anwser is:"+ans);
if (y == quiz[x].correct){
alert("Correct!");
}else{
alert("Wrong! \nThe right answer is "+quiz[x].correct);
}
关于javascript - 我想做一个 Javascript 测验,每次都会随机化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44015077/