【发布时间】:2015-01-09 21:49:21
【问题描述】:
我正在创建一个真/假事实游戏,它已经完成了,只剩下一件事。问题总是以相同的顺序出现,我不知道如何编码,以使问题总是以随机顺序出现。 感谢任何方式的帮助。
代码:
div id="QA">
<h2></h2>
<span id="buttons"></span>
<p id="mkttext"></p>
<p><strong>Points</strong> : <span>0</span></p>
<div id="demo"></div>
<br>
<div id="centerbutton">
<button id="restart" onClick="history.go(0)">Restart game</button>
</div>
</div>
<script>
var questionnaire = [
{
"question" : "Q: The earth is round",
"valid" : 1, // indicates the correct array number, use 0, 1...
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"question" : "Q: The correct answer is True",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"question" : "Q: Java = JavaScript",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"question" : "Q: Grapes explode when you put them in the microwave",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"question" : "Q: Shakespeare invented both chess and the basketball",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"question" : "Q: LoL stands for World of Warcraft",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"question" : "Q: The average human will shed 40 pounds of skin in a lifetime.",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"question" : "Q: A giraffe can clean its ears with its 121-inch tongue",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"question" : "Q: Nepal is the only country that doesn't have a rectangular flag",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"question" : "Q: The match was invented before the cigarette lighter",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"question" : "Game over, check your score below.",
}
];
var $qa = $('#QA'),
$question = $("h2", $qa),
$buttons = $("#buttons", $qa),
$points = $("p>span",$qa),
questionnaireLength = questionnaire.length,
qc = 0, // Current Question counter
points = 0; // Current points
function QandA(){
var quest = questionnaire[qc],
question = quest.question,
validIdx = quest.valid,
btns = quest.buttons,
answer = quest.answers;
$question.text( question );
//End of the game
if(qc >= questionnaireLength -1){
var link = document.getElementById('buttons');
link.style.display = 'none';
}
// generate buttons with text:
$buttons.empty();
var i=0; i<btns.length;{
$buttons.append("<button id='btnfalse'>"+ btns[i] +"</button>");
}
var i=1; i<btns.length;{
$buttons.append("<button id='btntrue'>"+ btns[i] +"</button>");
}
// Retrieve generated buttons
var $btn = $("button", $buttons);
// Assign click
$btn.one('click', function(){
var idx = $btn.index(this); // get button index
//var parent = document.getElementById('mkttext');
//var p = document.createElement('p');
//p.innerHTML = "Game says: "+ answer[idx] ;
//parent.appendChild(p);
points += (idx === parseInt(validIdx, 10) ? 1 : -0);
$points.text( points );
// Next question
qc++; QandA();
});
}
QandA();
</script>
【问题讨论】:
-
Juhana 发布了我喜欢使用的数组洗牌功能。我强烈推荐它。
-
只是洗牌你的阵列。数组中的顺序重要吗?
标签: javascript jquery html random