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 Create a while Loop

Robert O'Toole
Robert O'Toole
6,348 Points

while(counter<???)

it seems that for some questions if i want the loop to run 28 times this works:

while(count<29)

then other times this works:

while(count<28)

why is that? im still making sure to put the count +=1 and var count=0

can someone plz explain this? the past few quizzes seem to give me different reasons why code isnt working and to my knowledge it seems rather inconsistent :/

Can you provide complete code loop examples?

1 Answer

Armin Kadic
Armin Kadic
14,588 Points

Because if you only put a greater than sign ">" or less than "<" sign, the number you provided will not be included. So if you write "count < 26" this will make it count to 26 but not including 26. On the other hand, if you add an equal sign like this "count <= 26" , this will include the 26. The count starts from zero, so this "count <= 26" will count 27 times because it includes the zero and the number 26. This "count < 26" will count it 26 times because it gets the zero but not including the 26. I hope this clears things up for you :)