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



我想做一个测验,每次都随机化问题。我得到了一些随机化答案而不是问题的东西。这是我到目前为止得到的。

       <!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);
}

最新更新