[英]I want to make a Javascript Quiz that randomizes the questions each time
我想做一個測驗,每次隨機化問題。 我得到的東西可以隨機回答,而不是問題。 這是我到目前為止所得到的。
<!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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.