2017-12-21 15 views
0

私が学んでいるように、私はトリビアゲームの作成を任されています。私は本当に私が正しい道にいると思った。問題は、自分のゲームで「完了」をクリックすると、答えに関係なくです。彼らはすべて正しく戻って来ません。私はこの欠陥がどこにあるのか分かりません。誰かが驚くべきエラーを指摘できるかどうか。トリビアゲームが正しくスコアリングされない

$('#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>"); 

} 

}

+0

ワンノート: '$( '#wra')。prepend ...' – nurdyguy

+0

'value = '" + questions [i] .answers [j] + "'' <---- – epascarello

+0

jQueryオブジェクトを再利用しているためです。 'var wrap = $( '#wrap');'あなたはあなたが持っているjQueryのために '$ wrap .____'を実行できます。 – nurdyguy

答えて

0

あなたがそうquestions[i].answers[j]+" '

の間に自分の価値観の比較でそれぞれ正解に余分な「スペース」を追加気づけばあなたのエラーがライン

$("#wrap").append("<input type = 'radio' name = 'question-" +i+"' value = '"+questions[i].answers[j]+" '> "+questions[i].answers[j]) 

でありますその余分なスペースのために正解と選択された値が決して真実ではない

+0

感謝!それは、そのような単一のものがそれを台無しにすることがどのように狂っている。それは私の問題を解決した。 – iMaxPrime

関連する問題