Cant figure out where to place my conditions in my while loop! *Javascript*


#1

link to task: https://www.codecademy.com/en/courses/javascript-beginner-en-ASGIv/1/1?curriculum_id=506324b3a7dffd00020bf661#

Hey, so I tried to add my two conditions to my while loop but I cant seem to figure out where to place them.
The conditions are the count < 3; and count++;
Someone please tell me where im wrong.

this is my code:

,,,

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

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

loop();

,,,


#2

Hi there,

The while loop's parenthesis should contain the condition that you want to check. In this case we want to know whether our variable count is less than 3. If that condition is true the while loop will trigger and continue to fire until the condition is false.

The counter count++ simply just means that whatever the current value of count is, add 1 to it. Eg count = count + 1;

So on the next iteration of the loop the while conditional will test the new incremented count variable to see if the condition is still true.

while(count < 3) // While Checks to see whether count is less than 3.
{
    console.log("I'm looping!"); // This should print 3 times because the loop runs 3 times
    count++; // This counter can be placed anywhere inside the while block { } but some thought may need to be taken as to where. This may or may not affect the output you wish to achieve. 
}

If we were to visualize what is happening you could say it like this:

While checks to see if count(0) is < 3. It is, so the loop commences.
"I'm Looping"
Adding 1 to count. Count is now equal to 1.

While checks to see if count(1) is < 3. It is, so the loop commences.
"I'm Looping"
Adding 1 to count. Count is now equal to 2.

While checks to see if count(2) is < 3. It is, so the loop commences.
"I'm Looping"
Adding 1 to count. Count is now equal to 3.

While checks to see if count(3) is < 3. It is not, so the loop exits and something else can be done.

Hope that helps.


#3

Thank you very much!