JScript 确认无限循环
JScript confirm infinite loop?
var setOfCats = {}; //an object
while (r = true) //testing to see if r is true
{
var i = 0;
setOfCats.i = prompt ("What's your cat's name?", ""); //index object elements
alert ("Congratulations! Your cat has been added to the directory.");
var r = confirm ("Would you like to add another cat?"); //if r is true, then the loop should continue. if false, the loop should end.
i++
}
但是,循环不会结束。在过去的30分钟里,我一直在思考这个问题,徒劳无功。有什么想法吗?
您的评论不正确。
r = true
不测试r
是否true
;它分配r
成为true
。
您需要使用 ===
运算符比较变量。
或者你可以写while(r)
,因为r
本身已经是正确的。
while (r = true)
您正在设置r
以true
每个循环迭代。 你想要while (r == true)
,或者只是while (r)
。
为清楚起见,应在while
声明之外设置r
和setOfCats
:
var setOfCats = [];
var r = true;
while (r) {
setOfCats.push( prompt ("What's your cat's name?", "") );
alert ("Congratulations! Your cat has been added to the directory.");
r = confirm ("Would you like to add another cat?");
}
在每次迭代 while 表达式时,将 r 的值重新赋值为 true。 因此,它将始终覆盖该值。
您应该使用以下方法进行 while 测试:
while(r === true)
或更惯用:
while(r)
这应该有效:
var setOfCats = {}; //an object
var r = true;
while(r) //testing to see if r is true
{
var i = 0;
setOfCats.i = prompt ("What's your cat's name?", ""); //index object elements
alert ("Congratulations! Your cat has been added to the directory.");
r = confirm ("Would you like to add another cat?"); //if r is true, then the loop should continue. if false, the loop should end.
i++
}
相关文章:
- 使用滚动溢出-x进行无限循环
- 从index.html调用函数,该函数无限循环
- 使用jQuery无限循环播放HTML页面幻灯片
- Grunt任务没有加载,获得无限循环
- 如何在不进入无限循环的情况下将网站重定向到Facebook画布URL
- Backbone+RequireJS+Mediator模式导致视图逻辑短路和无限循环
- jQuery无限循环,动画化许多项目
- 如何避免试图用php+jquery显示php起始页的无限循环
- 6502仿真增强型基本无限循环$C000至$E0ED
- Angular ngRoute导致无限循环和堆栈溢出
- Jquery:无限循环和暂停
- 数组数组:无限循环
- MeteorJS使用流星调用和流星方法时的无限循环
- 如何无限循环数据集字符串
- jquery在无限循环中运行
- 遍历对象会导致无限循环
- 为什么内部Javascript循环会阻止外部循环中断(即无限循环)
- 角度编译指令似乎进入了无限循环
- 显示随机数js的无限循环
- JScript 确认无限循环