簡體   English   中英

JS-比較兩個數組中的兩個值似乎不起作用

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

誰能幫我這個代碼:

 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"); } } 

比較兩個數組的兩個插槽似乎不起作用:(謝謝!

prompt方法返回字符串,而答案是數字。 只需讓松散的比較通過用==代替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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM