7/13 error


#1

for (var i = 10; i >= 0; i+= 1) {
console.log(i);
}

whenever I try to run this, it freezes the page. I don't know why this is happening.


#2

Well if you have an error it often helps to think through it step by step. I know tedious work but at least it shows you the problem.

So you start with this part of your for loop:

var i = 10

means your setting i to be 10. Then you check if i >= 0:

i >= 0

it sure is 10 > 0. Then you print i and after this increase i by 1:

console.log(i);
 i+= 1

by now you might already see the problem:
I is increasing instead of decreasing meaning your condition i> 0 will always be true and therefore the loop will never stop working and even using more RAM by your browser can't solve this because it is not a matter of speed so it collapses after some time and good browsers show you a pop-up to stop the execution.

PS: This site might also be helpful:
http://www.pythontutor.com/javascript.html#mode=display


#3

Thank you this helped alot.