Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops What are Loops?

Elena Paraschiv
Elena Paraschiv
9,938 Points

Why does the program display 10 numbers if counter < 10

Shouldn't it display 9 numbers instead ?

2 Answers

Grace Kelly
Grace Kelly
33,990 Points

Hi Elena, it depends on where the counter is set at, if counter = 0 and the counter < 10 then we get 10 numbers as the count STARTS at 0 and finishes at 9, like so:

<!-- the numbers on the left are 0 - 9 (counter < 10) and the numbers on the right are the number of times the loop is ran -->

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

hope that helps!!

Elena Paraschiv
Elena Paraschiv
9,938 Points

Yes, you are right Grace :). Just realised it when I moved to the challange and the counter was set to 1. Thanks very much for confirmation

Grace Kelly
Grace Kelly
33,990 Points

No problem!! Glad you figured it out :)

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,546 Points

No, you will get 10 numbers because the initial variable is set at 0.

0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ==> 10 numbers.

I've done that a few times... we just have to remember that zero is a number. :)