Simple JS problem

Why does this code:

var y = 0 ;

while (y < 11) {
    console.log(y);
     y++;
};

output this:

0
1
2
3
4
5
6
7
8
9
10
10

Why are there two 10s and how would I avoid doing that using a while loop? I understand there’s other methods of doing this, but I’m just curious on how I would write this to avoid double 10s.

thanks!

It doesn’t. My console gave this:

0
1
2
3
4
5
6
7
8
9
10

Confirm for yourself

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>asdf</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
var y = 0 ;

while (y < 11) {
    console.log(y);
     y++;
};
</script>
</head>
<body>

</body>
</html>
1 Like

Oh weird. That was how it output in Codecademy. I guess their console is buggy in the lessons or something. Thanks

Codecademy does do that. I can confirm. Do not worry about it. They aren’t perfect (you’ll see that sometimes correct code is marked incorrect because they want it done a certain way)

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