简体   繁体   中英

JS - Comparing two values in two arrays doesn't seem to work

can anyone please help me with this code:

 var questions =[ ['how many states?', 1], ['how many continents?', 2], ['how many legs?', 3] ] var answers = []; var rightAnswers = []; var wrongAnswers = []; for(i = 0; i<questions.length; i+=1){ answers.push(prompt(questions[i][0]).toLowerCase()); if(questions[i][1] === answers[i]){ // rightAnswers.push(questions[i][0]) console.log("success!"); }else{ console.log("bummer"); } } 

Comparing two slots of two arrays doesn't seem to work :( Thanks!

The prompt method returns strings while your answers are numbers. Just let the loose comparison do the work by replacing strict === with == .

 var questions =[ ['how many states?', 1], ['how many continents?', 2], ['how many legs?', 3] ] var answers = []; var rightAnswers = []; var wrongAnswers = []; for(i = 0; i<questions.length; i+=1){ answers.push(prompt(questions[i][0]).toLowerCase()); if(questions[i][1] == answers[i]){ // rightAnswers.push(questions[i][0]) console.log("success!"); }else{ console.log("bummer"); } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM