Three loops error


#1


QUESTION:Write a while loop that logs "I'm looping!" to the console three times.

I dont know how to stop this infinte loop,Please help.
How do I stop the while loop so that it prints the text only 3 times?
Quite confused.


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

loop();


#2

you should increase number inside the whole loop (after console.log) so that number becomes 3, and the loop ends, which means I'm looping is logged to the console thrice.

Also, where do you create your number variable?


#3

Thankyou for the reply.

Is this what you meant?

CODE:

var number=0;

var loop = function(number){
while(number<3){
console.log("I'm looping!");
number>=4 = false;

}

};

loop();

I did well in for loops but got confused in while loops,could you please correct my code as I would understand where I'm doing wrong.


#4

the variable number is like a counter, you use it to count till three, so you can log i am looping to the console.

Then your while loop allows you to keep track of the count, but inside the while loop, you will need to increase number, otherwise number will just stay zero. you should know how to update variables


#5

So, having created a variable called number and set it to 0 outside the loop.

Then remember to include number++ into the actual loop. This allows it to keep track of the count and how many loops it's made. Without number++ in your actual loop then number will stay at zero.
.
Below is how it should look like using your variables and where the missing code is located....

var number = 0;

var loop = function(){
while(number < 3){
console.log("I'm looping!");
//*Enter missing code here*
}
};

loop();


#6

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