[英]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. prompt
方法返回字符串,而答案是数字。 Just let the loose comparison do the work by replacing strict ===
with ==
. 只需让松散的比较通过用==
代替strict ===
完成工作即可。
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"); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.