Practice makes perfect!


#1

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

I'm done with this exercise already, but I don't understand what's the point of using a global variable such as var count = 0?

further explanation to the exercise as a whole would be much appreciated! ^_^


#2

Problem is here: loop(count < 3);
You created function loop() which takes 1 parameter which should be a number. So you need:

loop(3);

In your case var count = 0 doesn't make sense because you have count as a parameter for function loop() and this variable exist inside the function loop(). Example:

var count = 1; // here you can put any number but in function scope will be the number which you put when calling loop()
var loop = function(count){
   while(count<4){
      //Your code goes here!
      console.log("I'm looping!"); // don't forget to put ; at the end of line
      count++;
   }
};
loop(3);
//Result: I'm looping!
//If you need I'm looping! 3  times:
loop(1);

But instruction says:

Write a while loop that logs "I'm looping!" to the console three times. You can do this however you like, but NOT with three console.log calls. Check the Hint if you need help!

So, you need:

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

In this case global variable var count = 0; makes sense because this variable exist inside the function loop().


#3

Thank you!

just what i was looking for :slight_smile:


#4

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