Practice Makes Perfect "Hey now"


#1

Using Google Chrome on both Mac and Windows 10. I've compared my script with successful versions, ad I can't see why mine fails.


"Oops, try again. Hey now--only one console.log() call!"


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


#2

Functions are meant to be run more than once, that's why we write them. They are re-usable code blocks. The above is not re-usable and can only run once. Why?

Because count is declared and initialized outside of the loop, and keeps its value after the function ends (it is a global variable). The next call to the function will do nothing since count will be 3.

The cure? Declare count locally so it gets set to zero every time the function runs.


#3

I have the same code and it does work. The next time it calls for count it doesn't equal 3 since you're only going up by one because of the count++.


#4

hi there can you explain to me how this code gets "a way to go "
var count=0;
var loop = function(){}

and am supposed to log three statements but i did not i just set the counter


#5

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