-2

So, I would like to know a simple way in which I could make the answers they submit in the prompt appear as either correct or incorrect preferably by writing it on the page.. Please help? Thanks.

<html>
<body>
<script type="text/javascript">
confirm("Are you ready?")

var score = 0;


var question = 
[
"2 + 2 = ?",
 "2 + 3 = ?",
 "5 + 5 = ?",
 "3 + 3 = ?",
 "6 + 5 = ?",
 "6 + 12 = ?"
 ];
question.sort(function(){
    return Math.round(Math.random());
});
 var answer =
 [
 "4",
 "5",
 "10",
 "6",
 "11",
 "18" 
 ];


for (var i = 0; i < 6; i++)
{
var prompt1 = prompt(question[i]);
}



</script>
</body>
</html>
  • Your shuffle method [is horrible](http://stackoverflow.com/q/13985083/1048572). Use a [proper one](http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array), you also will be able to customize it easily so that is shuffles two separate arrays in the same manner. – Bergi Jan 24 '14 at 00:52
  • I'm just beginning. Advice? – user3192324 Jan 24 '14 at 00:53

1 Answers1

0

It might be easier to group your questions and answers together

var QandA = [
                { "Q":"2 + 2 = ?",  "A":"4"  },
                { "Q":"2 + 3 = ?",  "A":"5"  },
                { "Q":"5 + 5 = ?",  "A":"10" },
                { "Q":"3 + 3 = ?",  "A":"6"  },
                { "Q":"6 + 5 = ?",  "A":"11" },
                { "Q":"6 + 12 = ?", "A":"18" } 
            ];

// you can then access them like this:
var question = QandA[0].Q;  // gets the first QandA object's question : "2 + 2 = ?"
var answer = QandA[0].A;    // gets the first QandA object's answer   : "4"

This is storing both the Question and the Answer in a single object {Q, A}. Then there is an array of these objects QandA

jasonscript
  • 6,039
  • 3
  • 28
  • 43