Introduction to While loops in JS-practice makes perfect


#1

Not sure what is wrong with my code!
//Remember to set your condition outside the loop!
var count=0

var loop = function(){
while(count<3){
//Your code goes here!
console.log(count++);
count>3;
}
};

loop();


#2

Try this

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

loop();

or this

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

loop();

#3

What kind of error do you get?

console.log(count++);
		count>3;

The second line is unnecessary but the rest seems to be ok.

PS: it also would make sense to put the var count =0; inside of the function but before the while loop.


#4

This was close... just need to log the "I'm looping!" text...

var count = 0;

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

#5

Hi, everyone
My code doesn´t work. Please, could you tell me what it is wrong

var count = 0;

var loop = function (displayMsg) {
while(count<3){
console.log(displayMsg);
count++;

//Your code goes here!
}

};

loop( "I´m looping" );


#6

@flavioibr

Please post this on a new topic with your question + error message + code if you are having a problem. No need of reviving an 5 months old topic.


#7

I can´t create a new topic, because I can´t write the category (The system doesn´t allow it)

The error message is

ops, tente outra vez. It looks like you're not printing the right string to the console.


#8

@flavioibr

When I'm talking about a topic, you go in the appropriate section in the forum. You see the icon next to the search bar on the upper right corner? You click on it and then it displays the different categories for which you can start your thread. Click on the button create new topic and edit it.


#9

Hi, everyone.

I see your function have a parameter called "displayMsg", which is not defined. Then you are logging into console that same parameter. Perhaps you could try the string "I'm looping!! there, instead. At the end, we can call the function which name is "loop" without any parameters, and it should be ok.

I've recently reach the solution. Thanks for all your contributions which help me to achieve it.

Greetings.


#10

Hi, designrockstar60179

I tried your solution, but it doesn´t work.

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

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

//Your code goes here!
}
};

loop ();


#11

Hi, everyone. I tried the solutions that I saw in old post, but it doesn´t either

https://www.codecademy.com/forum_questions/5084d780a789ad020000243e

I don´t know what is wrong. Maybe, the system was up-to-date, and it doesn´t accept the old solutions


#12

Hi this part

var loop = function ("I´m looping")

you should remove the "I´m looping" and it will work


#13

Thanks for your help, but unfortunatelly it still doesn´t work. Do you have another solution ?

var count=0;

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

//Your code goes here!
}
};

loop ();

The error message is:
Ops, tente outra vez. It looks like you're not printing the right string to the console.


#14

the error says you should check the string you are printing this one

"I´m looping"

between I and am you have a (´) instead of a (') shoud be like that

"I'm looping"

#15

Hi flavioibr,

You could try replacing the piece of code > var loop = function ("I´m looping"){

by the following : var loop = function (){

(We've erase the string "I'm looping).

Then you could replace the piece of code "console.log("I´m looping");"

by the following one:

console.log("I'm looping!");

(It seems is needed to change the ´ by the ' . Besides adding the ! symbol at the end is needed)


#16

Thank you designrockstar60179. Finally, I got it

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

loop(i);