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 trial

JavaScript

Won't the loop run 9 times, not 10?

var counter = 0; while ( counter < 10 ) { console.log(counter); counter += 1; } Won't the loop run 9 times, not 10? Because 10 can't be greater than 10.

2 Answers

This is something you're going to have to get used to--hehe. Zero is something and therefore counts as the first run!

counter = 0; // run once
counter = 1; // run twice
...
counter = 9; // run for the tenth time
counter = 10; // do not run for the 11th time

Cheers!

It is common to trace one's code to see how it is running. Try writing the same thing but console.log(counter) and then you can see the value of counter each time it iterates through the loop. If you get used to this and doing it to small subsets of numbers and then generalize the solution to a bigger set, it is much easier to see how the code is working.

Cheers!

O great, thanks.