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 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,858 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. :)