有人可以帮我解决我的问题吗,有警告:“看起来变量答案没有分配值 true。”
变量answer被赋予 boolean 值true。
我正在尝试修复,但我做不到。
var multiplesOfEight = [8, 16, 24, 32, 40];
var answer = multiplesOfEight[answer] % 8 !== 0;
var answer = function() {
for (i = 0; i < multiplesOfEight.length; i++) {
if (i % 8 !== 0) {
console.log("multiple of 8");
console.log(i);
} else {
console.log("X");
console.log(i);
};
};
};
answer();
这是我的代码的输出:
X
0
multiple of 8
1
multiple of 8
2
multiple of 8
3
multiple of 8
4
multiple of 8
5
从现在起谢谢你
最佳答案
定义函数答案时,您将覆盖变量答案
也就是说,您是否正在寻找类似的东西
var multiplesOfEightCheck = [8, 16, 24, 32, 40];
multiplesOfEightCheck.forEach(function(e) {
if (e % 8)
console.log("X");
else
console.log("Multiple of 8");
console.log(e);
})
关于javascript - 变量answer 未分配值 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31992179/