0

Javascript: Error in simple loop

I wrote the following code when I practiced loops: var num = 2; for (;num <= 100; num + 10) { document.write(num + "<br/>"); } What I thought I was doing: I thought I was assigning the value 2 to a variable named num. With the keyword "for" I initiate the loop. Statement1 I left out, because I already assigned the variable. So: for (; statement2, statement3) statement2 defines the conditions under which the loop runs So: var num = 2 for (; num <=100; statement3) The statement3 defines the statement to be executed at each operation until the statement2 is reached. So: var num = 2 for (; num <=100; num +10) This is followed by the instruction to write the result of each operation into the Html document net with a line break. So: { document.write(num + "<br/>"); All in all: var num = 2; for (;num <= 100; num + 10) { document.write(num + "<br/>"); } Well, but... It's not really working at the moment ^^ Bonus would be, if someone else can tell me how to avoid such crashes in the future. This will help you from learning when you have to reboot. I thought this would not happen in the secure environment of SL. The browser reports: A page is slowing down your computer, would you like to stop it? But whether I enter stop or wait, the PC freezes after and after. Happens to me on my own html page under Debian Linux and on the page of SL which I had opened with WIndows for testing. I would test it on a third system, but at the moment I am not allowed to access my wife's computer :D Well, he who does nothing, does nothing wrong. Any help or advice would be great.

20th Apr 2020, 4:32 PM
Sven_m
Sven_m - avatar
3 odpowiedzi
+ 1
The problem is that you are running into an infinite for loop. The solution is simple, for (; num <= 100; num + 10) Does nothing but checks if the number is smaller than 100 and then instead of adding 10 to num, executes num + 10 and continues. So no matter how many times you iterate, num = 2. So instead of num + 10, write num += 10. This will actually add 10 to num, and the value of num will increase by 10 in each iteration. So num will be 2, 12, 22, 32...... So the final statement would be for (; num <= 100; num += 10) OR for (; num <= 100; num = num + 10) (And for the record, even I hate it when you run into an infinite loop in JavaScript. If you run into an infinite loop in any other language , it runs smoothly and you can just press Ctrl + C to stop it. But in JavaScript running in browsers there is no way to stop it except for closing the tab, which you can't since your computer is frozen)
20th Apr 2020, 4:58 PM
XXX
XXX - avatar
+ 1
for (; num <= 100; num = num + 10) {...} You were just adding 10 to num on each loop, but never actually assigning the new value to num.
20th Apr 2020, 4:52 PM
Russ
Russ - avatar
0
Thank you very much for both your answers. I chose XXX as the "best" answer because it is very detailed and I understand the situation better. I have now also experienced the special behaviour of the language in case of faulty loops. Yes, it is "spezial". I will now continue with my studys. Thank you.
20th Apr 2020, 7:51 PM
Sven_m
Sven_m - avatar