#5, Why does this work? purpose of count++?


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

``` var count = 0;

var loop = function(){
while(count < 3){
console.log(“I’m looping!”);
count++;
}
};

loop();

<do not remove the three backticks above>

So i've figured out that this is the answer, but I am not sure why this works.
Without adding the line count++, it loops forever and crashes. But why does it stop it from looping forever?

I  thought that ++ was just the same as + 1.

#2

You can replace count++; with count = count + 1;.


#3

I understand that they are the same thing, but why does that stop the function instead of it making it go on forever? There is the condition (count < 3) indicating to run as long as its less than 3, so wouldn’t it stop even without count++?

Also, why are the () left blank for function and the last line “loop();”?


#4

No, because without count++ the value of count wouldn’t change each time, meaning it’s always < 3 meaning it will run forever.


#5

Because the loop function does not expect any parameter.


#7

Thanks much @jibblyj @eveat. Got it.


#8

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.