11. Review - Why am I logging a number at the end?


#1


Hello!,

I'm able to complete this excercise, but my do/while loop is logging a spare number at the end.

It ends up saying "I'm saying it 4 times. 4"

There shouldn't be a 4 by itself as far as I can tell... I'm just trying to figure out what I did to make it log the last value of x by itself.

Thank you!


// Write your code below!

for (i = 0; i < 5; i++){
    console.log("I is " + i +".");
}

var dog = true;
while (dog) {
    console.log("Dogs are nice.");
var dog = false;
}

var x = 3;

do{
    console.log("I'm saying it " + x + " times.")
    x++;
}
    while(x < 5);


#2

This is the last command seen by the console, so it responds by echoing the latest value. Put this before the console.log() line and it won't echo.

Don't use var inside a loop. The variable is already declared. All you need to do is redefine it.

dog = false;

#3

Thanks a bunch! And thanks for pointing out the variable in a loop thing. I wouldn't have caught that.


#4

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