简体   繁体   English

我想做一个Javascript测验,每次随机化问题

[英]I want to make a Javascript Quiz that randomizes the questions each time

I want to make a quiz that randomizes the questions each time. 我想做一个测验,每次随机化问题。 I got something which randomizes the answers but not the questions. 我得到的东西可以随机回答,而不是问题。 Here is what I got so far. 这是我到目前为止所得到的。

       <!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>

You can prompt for only once, then store in variable to choose all the quiz elements.. 您只能提示一次,然后存储在变量中以选择所有测验元素。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM