Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRyan Scharfer
Courses Plus Student 12,537 PointsWhere does the "1002" come from?
Hello, as an exercise from another source I wrote a little Javascript code to print all even numbers to the console from 2 to 1000. Below is my code:
var startNumber = 2;
while (startNumber <=1000) {
console.log(startNumber);
startNumber = startNumber + 2;}
Some how, 1002 is the last line printed to the console. I don't understand how that is. As you see, the largest the number can be and still satisfy the while statement is 1000. The number 1000 should be printed to the console, the variable is increased by 2, but then no longer satisfies the while statement so no further numbers should be printed... what am I missing?
5 Answers
Dino Paškvan
Courses Plus Student 44,108 PointsIf you're doing this in the Chrome console, you're misinterpreting the output.
The last number (1002
) is not an output from the while
loop, it's a debug output giving you the current value of the startNumber
variable which was increased to 1002
on the final iteration of the loop.
Try running this (concatenating the output with a string):
var startNumber = 2;
while (startNumber <=1000) {
console.log('My output: ' + startNumber);
startNumber = startNumber + 2;
}
and you should see a difference. Or try it in the Firefox console. Firefox will first output 1002
and then the output of the loop. Or try Node.js, and you'll just get what you expect.
Your loop is running the way it should be, it's just that the additional output in the console is confusing you.
Aaron Arkie
5,345 PointsI might sound dumb as i am not sure if my explanation is correct however, i believe since the computer counts from 0-9 rather than 1-10, in this case the 2 is at position 0 so you get an extra 1000+2 at the end. try setting it to
var startNumber = 2;
while (startNumber <=1002 // or 998) { console.log(startNumber); startNumber = startNumber + 2; }
Aaron Arkie
5,345 Pointssorry i forget how to make comments in java-script i'm more accustomed to java.
Aaron Arkie
5,345 Pointsi was correct i used the chrome console to compile your code and if you change it to 998 it ends at 1000. im not so sure how to make it stop at 1000 and you may need to add more code.
Ryan Scharfer
Courses Plus Student 12,537 PointsHi Aaron, Yea, I figured that out, too. I changed it to 998, but that doesnt make sense to me. I wanted to know why we have to do that. Your first explanation is a possibility.
Aaron Arkie
5,345 Pointsif you take out the equal sign out of startNumber<= 1000, it also stops at 1000 and prints it.
Dino Paškvan
Courses Plus Student 44,108 PointsNo, it really doesn't. It stops at 998
then. See my answer above.
Ryan Scharfer
Courses Plus Student 12,537 PointsRyan Scharfer
Courses Plus Student 12,537 PointsAwesome. Thanks Dino.