5. Practice makes perfect


#1


5. practice makes perfect


oops, try again. Are you logging "I'm looping!" to the console three times?


//Remember to set your condition outside the loop!
var count = 0

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

loop();


#2

Are you?

What information are you missing? Please explain what you are looking for, show what you've tried so far, explain what has got you stuck.


#3

im looking for any errors in my code that are not allowing "I'm looping!" to log because right now nothing is coming up


#4

What needs to happen in order for your string to get printed?

Your last line is to call your function. So you would want your function to run. That's something you can test. Did the function run? By adding a console.log at the beginning of the function, you can determine whether it did.
The next thing that needs to happen is for the loop condition to be satisfied. Is it? Since your string isn't being printed you can already say that no, it isn't. So if the function runs but the condition isn't satisfied, what then is the problem? Is the condition correct? What should the value of the variable used in the condition be? Which value does it actually have, how can you find that out?


#5

Your function has parameter count. So when you calling it the function expects that you will pass one argument. But do you really need this parameter?


#6

As @ionatan said

By adding a console.log at the beginning of the function, you can determine whether it did.

So lets do it:

var count = 0;

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

Result is undefined because parameter count is a local variable which exist inside the function. When you called the function you didn't initialize this parameter so count > 3 returns false and loop didn't run. But second log outside the function prints 0 because var count is a global variable.

So you can remove variable, but put 3 as an argument when calling the function. Or remove the parameter from function expresion and now var count can be a global variable which exist inside the loop().


#7

[quote="trayllyl000, post:1, topic:75529, full:true"]


5. practice makes perfect


oops, try again. Are you logging "I'm looping!" to the console three times?


//Remember to set your condition outside the loop!
var count = 0

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

loop();

call your function


#8

here my solution
//Remember to set your condition outside the loop!
var count= 0 ;

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

loop();


#9

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