【问题标题】:The variable answer isn't assigned the value true变量 answer 未分配值 true
【发布时间】:2015-11-06 15:42:01
【问题描述】:

有人可以帮我解决我的问题吗,有警告:“看起来变量答案没有被赋值为真。

变量 answer 被赋予布尔值 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

从现在开始谢谢你

【问题讨论】:

  • 我不确定你想用你的代码做什么。您能否发布您期望看到的输出?

标签: javascript html variables boolean variable-assignment


【解决方案1】:

当你定义函数 answer 时,你正在覆盖你的变量 answer

也就是说,您是否正在寻找类似的东西

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-27
    • 2022-10-05
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多