琐事游戏得分不正确
Posted
技术标签:
【中文标题】琐事游戏得分不正确【英文标题】:Trivia Game not scoring correctly 【发布时间】:2018-06-04 13:52:11 【问题描述】:所以在我学习的过程中,我的任务是创建一个琐事游戏。我真的认为我在正确的轨道上。问题是,当我在游戏中单击“完成”时,无论答案如何。他们都回来不正确。我不确定我的编码中的缺陷在哪里。如果有人能指出错误,那就太棒了。
$('#start').on('click', function()
game.start();
)
$(document).on('click','#end',function()
game.done();
)
var questions = [
question:"The Fantastic Four have their headquarters in what building?",
answers:["Stark Tower","Fantastic Headquarters","Baxter Building","Xavier Insitute"],
correctAnswer:"Baxter Building",
,
question:"Peter Parker works as a photographer for?",
answers:["The Daily Planet","The Daily Bugle","New York Times","The Daily Rag"],
correctAnswer:"The Daily Bugle"
,
question:"S.H.I.E.L.D.'s highest ranking agent is?",
answers:["Nick Fury","Captain America","Natalia Romanova","Peter Parker"],
correctAnswer:"Nick Fury"
,
question:"What vehicle is the Avengers primary mode of transport?",
answers:["A bus","The Quinjet","The Blackbird","The Blackhawk"],
correctAnswer:"The Quinjet"
,
question:"Ghost Rider is also known as?",
answers:["The Guardian Devil","The Spirit of Hate","The Spirit of Vengeance","The Red Skull"],
correctAnswer:"The Spirit of Vengeance"
];
var game =
correct: 0,
incorrect: 0,
counter: 10,
countdown: function()
game.counter--;
$('#counter').html(game.counter);
if(game.counter<=0)
console.log("Time is up!");
game.done();
,
start: function()
timer = setInterval(game.countdown,1000);
$('#wra').prepend('<h2>Time Remaining: <span id ="counter">120</span> Seconds </h2')
for (var i=0;i<questions.length;i++)
$('#wrap').append('<h2>'+questions[i].question+'</h2');
for(var j = 0; j<questions[i].answers.length;j++)
$("#wrap").append("<input type = 'radio' name = 'question-" +i+"' value = '"+questions[i].answers[j]+" '> "+questions[i].answers[j])
$('#wrap').append('<br><button id="end">DONE</button>')
,
done: function()
$.each($('input[name="question-0"]:checked'), function()
if($(this).val()===questions[0].correctAnswer)
game.correct++;
else
game.incorrect++;
);
$.each($('input[name="question-1"]:checked'), function()
if($(this).val()===questions[1].correctAnswer)
game.correct++;
else
game.incorrect++;
);
$.each($('input[name="question-2"]:checked'), function()
if($(this).val()===questions[2].correctAnswer)
game.correct++;
else
game.incorrect++;
);
$.each($('input[name="question-3"]:checked'), function()
if($(this).val()===questions[3].correctAnswer)
game.correct++;
else
game.incorrect++;
);
$.each($('input[name="question-4"]:checked'), function()
if($(this).val()===questions[4].correctAnswer)
game.correct++;
else
game.incorrect++;
);
this.result();
,
result: function()
clearInterval(timer);
$('#wrap h2').remove();
$('#wrap').html("<h2>All done!</h2>");
$('#wrap').append("<h3>Correct Answers: " + this.correct + "</h3>");
$('#wrap').append("<h3>Incorrect Answers: " + this.incorrect + "</h3>");
$('#wrap').append("<h3>Unanswered Questions: " + (questions.length-(this.incorrect+this.correct)) + "</h3>");
【问题讨论】:
一注:$('#wra').prepend...
value = '"+questions[i].answers[j]+" '
我还建议保存该 jQuery 对象,因为您正在重用它。 var $wrap = $('#wrap');
然后你可以为你拥有的任何 jQuery 做$wrap.____
。
@epascarello 我不明白你在说什么。我应该删除它吗?还是其中有问题?
@nurdyguy 这是一个很棒的提示!我将在我的学习中使用它
【参考方案1】:
你的错误就在这行
$("#wrap").append("<input type = 'radio' name = 'question-" +i+"' value = '"+questions[i].answers[j]+" '> "+questions[i].answers[j])
如果你注意到你在questions[i].answers[j]+" '
的每个正确答案上附加了一个额外的“空格”
因此,由于额外的空间,您的正确答案和所选值之间的值比较永远不会正确
【讨论】:
谢谢!像这样的一件事情怎么能把它搞砸,真是太疯狂了。它解决了我的问题。以上是关于琐事游戏得分不正确的主要内容,如果未能解决你的问题,请参考以下文章
Android游戏得分int通过servlet添加到access数据库中