I do not get what I have to do here. PLZ help


#1

We have to create a while loop from scratch but i don't know how. here a the instructions:
Write a while loop that logs "I'm looping!" to the console three times. You can do this however you like, but NOT with three console.log calls. Check the Hint if you need help!
Thank you!

var count = 0;

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

loop();


#2

The code in the loop will run, over and over again, as long as the variable (count) is less than 3.
count=0;
so it'll will run, over and over again.because while(count < 3) is always true .This will crash your browser.
If you forget to increase the variable used in the condition, the loop will never end.
so you have to increase the value of count.

make sense :slight_smile:


#3

var count = 0; //Adds a new global variable called count with the value 0.

var loop = function(number){ //Adds a new parameter that i called number.
while(count < number){ // Until count is less than the parameter number then,
console.log("I'm looping!"); // it prints "I'm looping!".
count++; // This is very important to add because it stops the while condition. It can also be add like
// count = count + 1;
}
};

loop(3); // I tell the function loop to print "I'm looping!" 3 times.


#4

Thanks for the reply Marco, I had no idea what to do either.


#5

hi..I was just trying to print out stars in this pattern.





  • *




But. i'm just getting a syntax error:unexpected identifier.please help me with the code.

for(var i=1;i<=5;i++)
{
    var output1=" ";
    for(var j=i;j<=5;j++)
    {
    output1+="*";
    }
    for(var k=1;k<(2*i)-1;k++)
    {
        output1+=" ";
    }
    for(var j=i;j<=5;j++)
    {
        output1+="*";
    }
    
    console.log(output1);
}

    
for(var p=2;p<=5;p++)
{
    var output2=" ";
    for(var q=(6-p);q<=5;q++)
    {
        output2+="*";
    }
    for(var r=2p;r<10;r++)
    {
        output2+=" ";
    }
    for(var q=(6-p);q<=5;q++)
    {
        output2+="*";
    }
    console.log(output2);
}

#6

for(var r=2p;r<10;r++) // what is the value of 2p? do 2*p

btw nice pattern :slight_smile:


#7

yeah..I have figured it out..Thanks for the help.:slight_smile:


#8

Hi..I have a small doubt regarding this code.`
var nyc = {
fullName: "New York City",
mayor: "Bill de Blasio",
population: 8000000,
boroughs: 5
};

// write a for-in loop to print the value of nyc's properties
for(var prop in nyc){

console.log(nyc[prop]);

}

the above code works fine but it doesnt work when ```console.log(nyc.prop);

type or paste code here

why is that?


#9

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