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 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,908 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Tobiasz Gala
Full Stack JavaScript Techdegree Student 23,529 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