2016-06-24 4 views
1

Das Programm fügt nicht die richtigen Antworten am Ende der Seite hinzu. Ich weiß nicht warum, auch wenn die Prompts auf Chrome korrekt beantwortet werden, mit Begrenzungen, die bedingte Anweisung fügt sie nicht zu questionsRight hinzu.var questionsRight fügt keine richtigen Antworten hinzu

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

Hallo bitte eine Antwort wählen, wenn wir Ihnen helfen. –

Antwort

0

Die Bedingung verwendet nicht die richtige linke Zuweisung.

Demo: 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

Weil Sie vergleichen (Frage === Antwort) .Dieses sollte (Antwort === Antwort).

Verwandte Themen