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 Refactor Using a Loop

Amber Kuo
Amber Kuo
1,390 Points

How do numbers work in a loop counter?

For this challenge, in order to log on the console even numbers from 2-24, a for loop is used. How does the numbering here work? Why is it- for (i = 2; i < 25; i += 2);

and not- for (i=0; i ≤ 24; i += 2);


2 Answers

dragos busuioc
dragos busuioc
24,131 Points

for (i=0; i ≤ 24; i += 2); start the iteration from 0,so will console log even 0, and the chalenge is to console log the numbers from 2 to 24,not from 0 to 24

Tobiasz Gala
Tobiasz Gala
Full Stack JavaScript Techdegree Student 23,517 Points
 i = 0 // first you declare variable which is used as a counter
i <= 24 // you add condition so if i is less or equal to 24 loop will run
i += 2 // you tell for loop what to do when each loop ends... you increase value of i by 2

i +2 is the same as i = i + 2

In the challenge they ask for even number so the counter must start with the first even number which is 2