제가 배우는 동안, 나는 퀴즈 게임을 만드는 임무를 받았습니다. 나는 내가 옳은 길에 있다고 생각했다. 문제는 내 게임에서 "완료"를 클릭해도 답변에 상관없이입니다. 그들은 모두 잘못 돌아 왔습니다. 결함이 어디에서 발생했는지 모르겠습니다. 누군가가 놀라운 오류를 지적 할 수 있다면.퀴즈 게임이 올바르게 채점되지 않았습니다.
$('#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'). 앞에 붙이십시오 ... ' – nurdyguy
'value =' "+ questions [i] .answers [j] +"''<---- – epascarello
jQuery 객체를 재사용하고 있기 때문에 jQuery 객체. 'var wrap = $ ('# wrap'); 그러면 당신은 당신이 가지고있는 jQuery에 대해'$ wrap .____ '을 할 수 있습니다. – nurdyguy