javascript - JS - 比较两个数组中的两个值似乎不起作用
问题描述
任何人都可以帮我解决这个代码:
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
方法返回字符串,而您的答案是数字。===
只需通过将严格替换为 来让松散比较完成工作==
。
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");
}
}
推荐阅读
- javascript - Vue.js:如何根据数据表行中项目的值更改图标和背景颜色?
- windows - 从 docker(linux 容器)访问 Windows COM 端口
- oracle - Jasperserver 和 oracle 自治数据库
- optimization - 最大维矩阵
- c# - 可空引用类型和实现接口
- cakephp - CakePHP 4 Forms:不要在多选时预选
- json - 将带 href 的超链接添加到命名格式 I18n vuejs
- javascript - 如何将文本从 span 复制到没有 ID 的数据组属性?
- multithreading - .join() 方法即使在新线程上调用也会阻塞 UI 线程
- cookies - 如何在 jmeter 中传递 cookie(浏览器 cookie 和同时手动添加 cookie)