When to 'while' and when to 'for'


#1

I can't figure out what the problem is with my code

var count = 0;

var print = function(){
while(count < 3) {
console.log("I'm looping!");
count++;
};
};
print()

var print2 = function(){
for(var i=0; i=10; i++){
console.log(i);
};
};
print2()


#2

So i found the problem but I don't understand WHY it was a problem. I changed i=10 to i<10. Can anyone explain this to me?


#3

Well i = 10 means assign the value 10 to the variable i. This whole statement has a value of 10 and if you google a bit you might find this:
http://www.sitepoint.com/javascript-truthy-falsy/
Which tells you that all numbers except 0 are treated as false when used in a condition. So by using = instead of == or === you effectively created an infinite loop. And to your title:


or was this just the title of the exercise? Then maybe use the number and your error message instead :smile: