[英]Nooblyf, javascript, simple loop controlled by 3 variables, not sure why it stops looping after 1 iteration
Solved!解决了! ,
,
set var looplim = 9设置 var looplim = 9
and the overwrite looplim, to looplim = looplim + 8, instead of setting it to infinite, which was continually decreasing in value, which was reducing the value of looplim, but no longer!并且将looplim覆盖为looplim = looplim + 8,而不是将其设置为无穷大,它的值不断减少,这正在减少looplim的值,但不再!
learning to code from codecademy in javascript atm, have some experience with python that I need to completely relearn学习在 javascript atm 中从 codecademy 编码,对 python 有一些经验,我需要完全重新学习
Ok, so I wanted to create an example of a potential infinite loop, being limited by, multiple variables.好的,所以我想创建一个潜在无限循环的示例,受多个变量的限制。 This program basically has 3 retained variables that are both increasing in value, 2 of which are added together, the other is squared.
这个程序基本上有3个保留的变量,它们的值都在增加,其中2个相加,另一个是平方。 Once the squared value gets larger than the added values, the loop stops!
一旦平方值大于相加值,循环就会停止! For some reason, the loop only runs once, when it should run 7 times, would really appreciate any advice!
出于某种原因,循环只运行一次,当它应该运行 7 次时,非常感谢任何建议!
http://hastebin.com/noxumojemo.vhdl http://hastebin.com/noxumojemo.vhdl
var looplim = 1;
var infinite = 8;
var k = 1;
//not sure if I actually need to pass variables to function like this, although, fairly certain i am.
var generator = function(infinite,looplim,k){
//value of infinite will be slowly increasing, until squared value of k is greater, and subtracted.
while(infinite >= 8){
console.log("mobiasMobiasMobias");
//value of k, is retained, and incremented by 1.
k = k + 1;
//looplim increases, by + 8 basically, and is retained.
looplim = looplim + infinite;
//loopdestroyer....should be a local variable here, and does not really need to be retained since value k is being passed to it, which is retained.
loopdestroyer = k*k;
//new value of infinite is set and retained, the increasing value of looplim, stays above loopdestroyer, for 7 cycles, im pretty sure, unless i'm mathing wrong, looplim being 57, and loopdestroyer being 64, when the loop ends.
infinite = looplim - loopdestroyer;
}
};
generator(infinite,looplim,k);
Unless I'm reading your code wrong, infinite is getting set as 5 in the first loop除非我读错了你的代码,否则在第一个循环中无限被设置为 5
var looplim = 1;
var infinite = 8;
var k = 1;
var generator = function(infinite,looplim,k){
while(infinite >= 8){
k = k + 1; // k = 2
looplim = looplim + infinite; // 1 + 8 = 9
loopdestroyer = k*k; // k = 2 = 2 * 2 = 4
infinite = looplim - loopdestroyer; // 9 - 4 = 5
}
};
generator(infinite,looplim,k);
At the end of the first loop k
is 2 which makes loopdestroyer
hold the value 4, looplim
is 9 here (4*4 + 1) so looplim - loopdestroyer
gives the value 5 with which infinity
is set.在第一个循环结束时,
k
是 2,这使得loopdestroyer
保持值 4, looplim
在这里是 9 (4*4 + 1),所以looplim - loopdestroyer
给出了设置为infinity
的值 5。 That makes infinity
smaller than 8 so the loop ends.这使得
infinity
小于 8,所以循环结束。
Please use console.log
extensively, it helps.请广泛使用
console.log
,它会console.log
帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.