Question 5/11


#1

hello, every time I run this code it keeps on coming back to me with the same error: "Are you sure that you are logging 'I'm looping!' to the console 3 times?"

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

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

loop(3);

Please, if you have the time can you help me out.
Thank you in advance.


#2

You're adding a lot of extras that you don't need, essentially get rid of the entire function, all you need is this:

var count = 0;

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

#3

The function is part of the exercise, so if it should be removed then it's an issue with the exercise.


#4

Hey mc_yemi_ako,

var count = 0;

var loop = function(){ // You don't need anything in the parenthesis
    while(count < 3){
        console.log("I'm looping!");
        count++; // You weren't incrementing count
    }
};

loop(); //You shouldn't need a number here in the loop

#5

Thank you very much for this!!


#6

@designguychris, is it possible you could go explain this whole piece of code, and explain what each part means and what each part is for? (I'm a little confused as to why some parts are blank, like the function(), why is there nothing in the parenthesis? And what is the point of the loop(); at the bottom? Is it an input gateway into the function?) Basically, I need a template or an exemplar version, like an empty version of the code, but with the meanings/uses of the code written next to it so that if I ever need to look at it again and write the code, I know what to do and to adapt it to whatever I want! I am a beginner this is the hardest bit of coding I have come across so far!
Thank you so much in advance!!


#7

Hello!
I got the question correct but I do not understand it.. I was wandering if you could please explain better.
for example

var number = 0
var loop = function(){

while(number<3){ (I do not know why we use this statement to say that the number is less that 3 when we want the number to be copied 3 times)
    console.log("I'm looping!");
    number++;

and re we not here to write an opposite variable maybe(Number = 1) so that it does not write to infinity
}
};

loop();

I am really confused and I really need help understanding


#8

Well go through it step by step:
we start with number = 0 (maybe insert this inside the function before the loop so that it is reset with every function call).
Now we check the condition of the while loop number which is 0 should be less then 3. Obviously 0 is less than 3 so we execute the loop:

print string
increase value of number by 1 so now it is 1.

Check the condition of the loop again. Is number less than 3? Yes 1 is still less than 3.

print string
increase value of number by 1 so now it is 2.

Check the condition of the loop again. Is number less than 3? Yes 2 is still less than 3.

print string
increase value of number by 1 so now it is 3.

Check the condition of the loop again. Is number less than 3? No 3 is equal to and therefore no longer less than 3 so the condition is false and you stop looping. When you count the number of printed messages you see one for 0,1 and 2 so you made 3 outputs.


#9

OO!! so it startedfrom zero?? and it prints 0,1,2,3
I taught it printed 1,2,3 thats why i taught it should have been <=3 inttead of <3


#10

It works with the function, like you were trying to do, if use it like this:

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

loop(0);
//code

But really, it seems like placing the incomplete function to work with this exercise a bit uneccessary...


#11

var number = 0
Well you set it to start from 0 so yes it starts from 0 :slightly_smiling:

The nice thing if you start from 0 is exactly this behaviour: if you want to loop x times your initial value is 0 and your condition is counter < x. Also in really many programming languages the array and string index starts at 0 so it comes rather "natural" for the language to start at 0.