所以我试图做一个做某事的循环,然后当它完成时将var设置为false然后移动到下一个循环(或以其他方式退出并结束循环)。代码:
var loop = true;
while (loop = true)
{
console.log("whatevers");
loop = false;
}
while (loop = false)
{
console.log("meh");
//continue endProgram;
}
那我在这里做错了什么?
答案 0 :(得分:2)
=
只是作业,其中==
或===
用于比较。赋值变量被赋值为赋值后的表达式(参见@Oka注释)。例如:(loop = true
=> true
而loop = false
=> false
)请改为使用==
或===
:
var loop = true;
while (loop == true)
{
console.log("whatevers");
loop = false;
}
while (loop == false)
{
console.log("meh");
//continue endProgram;
}
答案 1 :(得分:0)
Try this way
-------------------------------------------------------------------
var loop = true;
while (loop)
{
console.log("whatevers");
loop = false;
}
while (!loop)
{
console.log("meh");
//continue endProgram;
}
答案 2 :(得分:0)
while(loop)与while(loop == true)相同..但是loop = true意味着你将变量循环设置为true。所以试试这个:
var loop = true;
while (loop)
{
console.log("whatevers");
loop = false;
}
while (!loop)
{
console.log("meh");
}