简单的JavaScript测验?

时间:2014-01-24 00:49:00

标签: javascript

所以,我想知道一种简单的方法,我可以使他们在提示中提交的答案显示为正确或不正确,最好是将其写在页面上..请帮助?感谢。

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

1 个答案:

答案 0 :(得分:0)

将问题和答案分组可能更容易

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"

这是将问题和答案存储在单个对象{Q, A}中。然后有一组这些对象QandA