【发布时间】:2021-04-20 22:08:53
【问题描述】:
所以我有这个代码:
function step3() {
//finds both answers
answer1 = top1 / (2*a);
answer2 = top2 / (2*a);
console.log(answer1 + " " + answer2);
solutions = isNaN(answer1)
console.log(solutions)
if (solutions = true) {
console.log("no real sol")}
step4()
}
当我运行它时,如果 answer1 是 NaN,它会打印 true,但每次它都不会打印出真正的 sol。为什么这样做?我认为只有在解决方案为真时才会这样做。
【问题讨论】:
-
您不需要为
true或false明确检查布尔值。if(solutions)就是你所需要的。 -
if (solutions = true)不检查solutions是否等于真,它是设置solutions等于true。你想要if (solutions === true)(或==,这取决于你想要什么类型的相等检查。 -
除了@ScottMarcus 的评论之外,您还使用了赋值运算符 (
=) 而不是相等运算符 (==)。 -
了解
=、==和===之间的区别
标签: javascript