2016-06-24 4 views
1

プログラムは、ページの下部に正解を追加していません。私はなぜクロムのプロンプトが正しく答えるとしても、キャップを付けても、条件付きのステートメントはそれらをquestionsRightに追加していないことはわかりません。var questions正しい答えが追加されていません

var quiz = [ 
    ['What is the capital of New York', 'Albany'], 
    ['What is the Capital of California', 'Sacramento'], 
    ['What is the capital of New Jersey', 'Trenton'], 
    ['What is the capital of Virginia', 'Richmond'] 
]; 

var questionsRight = 0; 
var questionsWrong = 0; 
var questionsRightList = []; 
var questionsWrongList = []; 

/* Don't NEED to declare the following right away, but it's good practice to go ahead 
and declare the variables you're going to use. Notice you don't need to assign any 
default value to them */ 
var question; 
var answer; 
var response; 
var html; 

function print(message) { 
    document.write(message); 
} 


    for(var i = 0; i < quiz.length; i++) { 
     // Get the first item from array[i] and set it as question 
     question = quiz[i][0]; 

      // Get the second item from array[i] and set it as answer 
     answer = quiz[i][1]; 

     // Get input from the user and set it to the response variable 
     response = prompt(question); 

     if (question === answer) { 
      //questionsRight is initially 0. If response === answer    questionsRight = 0+1 
      questionsRight += 1;  
     } 
     // else { 
     // //questionsWrong is initially 0. If response === answer questionsWrong = 0+1 
     // questionsWrong += 1; 

     // } 
    } 

    html = "You got " + questionsRight + " question(s) right."; 

    print(html); 
+0

こんにちは答えを選択してください。 –

答えて

0

条件に正しい左の割り当てが使用されていません。

デモ:私たちはあなたを助けている場合https://jsfiddle.net/y4orwqqu/

var quiz = [ 
    ['What is the capital of New York', 'Albany'], 
    ['What is the Capital of California', 'Sacramento'], 
    ['What is the capital of New Jersey', 'Trenton'], 
    ['What is the capital of Virginia', 'Richmond'] 
]; 

var questionsRight = 0; 
var questionsWrong = 0; 
var questionsRightList = []; 
var questionsWrongList = []; 

/* Don't NEED to declare the following right away, but it's good practice to go ahead 
and declare the variables you're going to use. Notice you don't need to assign any 
default value to them */ 
var question; 
var answer; 
var response; 
var html; 

function print(message) { 
    document.write(message); 
} 


    for(var i = 0; i < quiz.length; i++) { 
     // Get the first item from array[i] and set it as question 
     question = quiz[i][0]; 

      // Get the second item from array[i] and set it as answer 
     answer = quiz[i][1]; 

     // Get input from the user and set it to the response variable 
     response = prompt(question); 

     if (response.toLowerCase() === answer.toLowerCase()) { 
      //questionsRight is initially 0. If response === answer    questionsRight = 0+1 
      questionsRight += 1;  
     } 
     // else { 
     // //questionsWrong is initially 0. If response === answer questionsWrong = 0+1 
     // questionsWrong += 1; 

     // } 
    } 

    html = "You got " + questionsRight + " question(s) right."; 

    print(html); 
2

あなたは比較しているので(質問===答え)。これは(応答===答え)にする必要があります。

関連する問題