Question about output of loops


#1

so if I have a loop like this

// Write your code below!
for (var i = 1; i < 7; i++) {
    console.log("WEEEEEE");
}
while(i < 9) {
    console.log("WOOOOHOOOO")
    i++
}
do {
    console.log("ok done now")
    i++
} while(i < 11)

What I expect is it outputs "WEEEEEE" a few times "WOOOOHOOO" 2 times and then "ok done now" twice. for some reason it says the value of i at the end. so my output is

WEEEEEE
WEEEEEE
WEEEEEE
WEEEEEE
WEEEEEE
WEEEEEE
WOOOOHOOOO
WOOOOHOOOO
ok done now
ok done now
10

Does anyone know why this happens? Thanks :smiley:


#2

It's something Codecademy does, ignore it.


#3

To make it easier for you to get a direct result without having to bother about it that much, this track uses the console as output (normally it is more a debugging tool). Unfortunately the console has some features of its own and one is to echo the last used value, so what you see is the value of this statement i++. You might have used this on the first exercise where you just entered your name and saw it on the screen without using console.log or anything or when you use a function call on the last line instead of console.log(functionCall());. As @ionatan said it is nothing you need to worry about and the exercises can distinguish between this and a regular output.

PS: if it really bothers you, you can switch the position of i++ and console.log("ok done now") because console.log has no value of it's own (undefined) and is therefore not displayed in the console.