# While and do/while loops print my while condition

#1

function square(x) {
return (x*x);
}
for (var i = 0; i <= 20; i++) {
console.log(square(i));
}

var j = 0
while (j < 21) {
console.log (square(j));
j++;
}

var k = 0
do {
console.log(square(k));
k++;
} while (k <21);

This works fine. i get the squares of 0 - 20 printed to the console. My question is why for the while and do/while loops does the console also print a 20 at the end of the list?

289
324
361
400
20

This also happened on the exercise to create my own for and while loop before do/while loops were introduced. I had the console sing 99 bottles of beer on the wall and when using the while loop, I would end up with a single number 1 at the bottom of the console like this:
2 bottles of beer on the wall, 2 bottles of beer,
take 1 down, pass it around 1 bottles of beer on the wall. (<==== have to fix that "1 bottles" still)
1 bottle of beer on the wall, 1 bottle of beer,
Take that one down, pass it around, there ain't no more freaking beers on the wall!
1

Am I doing something incorrectly that's causing the limiting condition of my loops to print out to the console? If it's not incorrect, then what can I change to prevent that from printing?

#2

What makes you say they do? Maybe something else printed that.

#3

I'm saying that because in both of these examples, what printed on the final line below a finished loop was the number I set as the limiting condition on that same loop. If I changed the while loop in my example to ( i < 21; it would still print 20 at the end of the while loop. If I changed my do/while loop to while (k <= 20) to (k <= 10), it will certainly print:

0
1
4
9
16
25
36
49
64
81
100
10

There are also times when even the code that is provided as examples in while and do/while loops lessons result in the printout to the console.log of a string with a line printed underneath the string that says false or true depending on the condition set in the loop.

I get that this condition is being met and that is why the loop is ending, my question is why is it printing and how can I prevent that?

#4

Sorry I was responding to your title while ignoring most of your post.

While and do/while loops print my while condition

They are not printing it, so something else is. It's out of your control, it's something the codecademy environment is doing.

#5

Thank you. So outside of the codeacademy environment, I shouldn't expect that to print.

Much appreciated.

#6

Yeah, so if I ran it from my terminal..

``````nate@acine >> cat mycode.js
function square(x) {
return (x*x);
}
for (var i = 0; i <= 20; i++) {
console.log(square(i));
}

var j = 0
while (j < 21) {
console.log (square(j));
j++;
}

var k = 0
do {
console.log(square(k));
k++;
} while (k <21);
nate@acine >> node mycode.js
0
1
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
289
324
361
400
0
1
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
289
324
361
400
0
1
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
289
324
361
400
nate@acine >>``````