5.practice makes perfect!


#1

hi all,

please help!

my code

var count = 0;

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

}
loop(false)

};

loop(true);


#2

Hi remove the count = 0 inside the while condition and remove the loop(false)
and the loop outside is the call fucntion so you dont need to put anything inside in this case.
So your code should be like that

var count = 0;

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

loop();

#3

I believe your issue is when you made it so that loop is false and then true outside of the function.

var count = 0

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

loop();

This is what my code would look like @ravindramukund, now what this does is sets counts value first, in the while loop only needs one condition, then when after the code "console.log"s increases count by one and starts it over again until count = 3, then calls on the loop function but because it had no variable in the parentheses in the function it has none in it now.


#4

thank you @wizmarco and @claradavies.


#5

Hi

Why should the var count = 0 go outside the loop? Doesn't 0 mean false? I thought the condition true should go outside.


#6

Hi in this case we want to count through 0 to 5 so you need to declare the variable and set it to 0 so when you will run it will count 0 to 5 so the count = 0 doesn't mean false.


#7

Hi, thanks for posting this clear answer for everyone to check their work, much appreciated! I do have some questions as to why this answer is correct. I'm looking for clarification on why the loop function does not have anything inside its parenthesis.

Also, slightly on topic, slightly off topic.. In one of the previous exercises, the variable (understand) and in this one (count) is set to (true) or (0 in this case) at the very beginning, outside of all the loops and everything. What does this accomplish? Is it just a starting point? For example: we will begin this loop on true and it will cycle until we get to a false command, or we will start at 0 until we reach 3. Is that how that works?

I apologize if these questions have been answered already or if they are super simple.. I am very tired :grin: any and all help is greatly appreciated!


~MIKE


#8

Hi don't need to apologize I will answer you as Clear as I can .
Yes, the variable we declar outside is a starts point

In the case where we have understand = true
Will run just once and stop because just after console.log we declare false to stop it OtherWise it will be infinit while.

In the case we declare count = 0 the code will run 3 time like that

1 The code will run as long as count is Less than 3

while ( 0 < 3 )
while ( 1 < 3 )
while  ( 2 < 3 ) 
// And it will end

Hope I could help you.


#9

Great, thanks! And what is the reason for the loop function not having anything inside its parenthesis?

var loop = function()



~MIKE


#10

I was wondering the same thing... Could someone please explain?


#11

Hi @mikegl sorry if I took too long for respond you.
@blogsolver76842 hope that could make you understand it.

Here
A function can be creat with some argument like that

var sayHello  = function(name) {
    return "Hello , my name is " + name 
}

In this case we have to call the function with a with somthing inside the call function like that

sayHello("wizmarco")

#output 
Hello , my name is  wizmarco

But in our case it was wihout a argument like that

var quarter = function() { 
    console.log("I' dont have a argument")
}

And then if we call the the function quarter in this case we can't put anything inside the call function because the function dont have any argument you just need to call like that

quarter();

#output

I' dont have a argument

#12

Hi claradavies,

This is probably a very silly question and something I should have picked up long ago:

Why is there no parameter in function( )? I know it doesn't work in this but shouldn't it be
var loop = (count) {
while (count <3) {
console.log ("I'm looping!")
count++;
}
};
loop( );

Thanks in advance!


#13

This topic was automatically closed after 24 hours. New replies are no longer allowed.