JavaScript:为什么boolean总是返回true
JavaScript: why is boolean always returning true
当我用customerNumber的任何值运行它时,它总是返回if语句中的第一条警报消息。这里怎么了?
var customerNumbers = 13;
var winningNumbers = [];
winningNumbers.push(12, 17, 24, 37, 38, 43);
var match = false;
for (i=0; i<winningNumbers.length; i++) {
if (winningNumbers[i] == customerNumbers) {
match = true;
}
}
if (match = true) {
alert("This Week's Winning Numbers are:'n" + winningNumbers.toString() +
"'nThe Customer's Number is:'n" + customerNumbers + "'nWe have a match and a winner!");
}
else {
alert("This Week's Winning Numbers are:'n" + winningNumbers.toString() +
"'nThe Customer's Number is:'n" + customerNumbers + "'nSorry, you are not a winner this week.");
}
您不是在比较布尔match
和true
,而是将true
分配给布尔match
。您需要使用double或triple=。
match = true // Sets match to true
match == true // Compares match to true
match === true // Strictly compares match to true
所以,再加上一些等号,它应该会起作用。
不要在if语句中使用赋值运算符(=
)。使用相等运算符(===
)。
if (match === true)
相关文章:
- 对于loop.if-仅在经过所有间隔后返回true
- 如果函数返回True,则显示Javascript按钮
- Javascript If else 只返回 TRUE 或只返回 FALSE
- 过滤器返回true或false
- 我对“;返回true"嵌套函数内部;t工作
- 如何在成功输入时使“返回TRUE”
- 检查一个元素是否有一个类与另一个类总是返回true
- JavaScript函数未返回true
- 如果URL中有任何字符串变体匹配,则返回true
- 如何使以下函数返回true或false
- 使用“in”关键字检查 0 始终返回 true
- hasOwnProperty 在对照父对象属性进行检查时返回 true
- 为什么 isNaN 总是返回 true
- _.数组中的每个查找值都返回 true 或 false.使用下划线 JS
- 错误地返回true
- 为什么asp:LinkButton服务器端事件没有'当clientClick返回true时,t激发
- 阻止表单提交 javascript 为什么有一个明显的错误语句返回 true
- Node js命令器args返回true而不是值
- 如何将html表单与php链接,以便在例如单击按钮时返回true
- 从OnClientClick调用函数时,在JavaScript中返回true