While Loops


#1

Can't figure out what to do here. Here is the instruction:

"If we create a variable called count and set it to 0 outside the loop (on line 2), then do count++ each time we console.log() inside the loop, we'll be able to track how many loops we've made. If we set the while condition to be count < 3, that should do the trick!"

Here is my code (not sure what to change and how to change it:

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

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

loop();

Thanks in advance!


#2

Your suppose to increment count by 1 inside while loop as said in instructions so like this:

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

loop();

#3

just tried, still no luck :frowning:


#4

Maybe have a look at this explanation of all kinds of loops:


#5

okay I got it, thanks so much!


#6

Could you please tell me what the function is doing here?
I'm confused.


#7

Sure no problem!, i will comment inside the code by each line to make you see whats happening inside there!

 var count = 0 // You know this, your instatiating a variable called count to 0.
 var loop = function() // creating a function called loop.
     {
         while (count < 3)
         /* So here its saying while count <3 do stuff at bottom. 
    basically an if statement combined with a for statement. Has a condition for a 
For statement. Repeats until   condition is false. Note: So can easily 
make an infinte loop out of a while loop*/
         {
             //Your code goes here!
             console.log("I'm looping!"); //Both happenes if count >3
             count++;
             /* Makes count go up by one. so it runs only 2 times since it goes to 0,1,2.
                then it dosent run the third time since count is not less than 3*/
         }
     };

 loop();

#8

Oh, of course! Thank you so much! :smile:


#9

Hey guys, everything in that code makes sense except for the last line that says loop(); could someone explain what this does or perhaps guide me to a source that can explain?


#10

It calls the function, so the function actually runs. loop is the name of the function its going to run. You need to call a function so it actually runs.

So this is the syntax for that

functionName();


#11

Thank you very much!


#12

I still lost on why we needed the function() in the code since there was no parameters involved in the function


#13

You have to follow the Javascript rules!


#14

can you please elaborate what you mean by that.


#15

when I ran var count = 0;

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

I still got the same answer, so what did adding var loop = function() do to the code?


#16

It let's you repeat the process more easily by just adding loop() another time instead of copy&pasting the whole loop.


#17

someone help me please i dont know what to hier what is wrong whit that code?


#18

var getToDaChoppa = function(){
do {
console.log("i just want it so bad thats all");
}
while(getToDaChoppa);

};

getToDaChoppa();


#19

Hi first here how a do while work

var x = 0;
do {
    console.log("I'm looping");
    x++

} while(x < 2)

#output 

I'm looping
I'm looping

In your case they aske your to put It inside the function getToDaChoppa, you just need to put inside the function like that.

var x  =  0;

var  getToDaChoppa  = function() {

    // Here go the do while 

}
getToDaChoppa()

#20

I tried it this way and it worked. Maybe a refresh or change browser?