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 The Refactor Challenge, Part 2

Writting correct code of even numbers from 2 to 24 using a loop to the JavaScript

for(var i=2;i=24 i+=2)
console.log(i);
jason chan
jason chan
31,009 Points

I've edited your post for formatting.

3 Answers

Ryan Parcell
Ryan Parcell
12,625 Points

Aurelio's answer is correct with one minor oversight. As written, this would only log even numbers from 2 to 22. To log even numbers from 2 thru 24 you need to make the conditional < 26. for(var i = 2; i < 26; i += 2){ console.log(i) }

There are a few things wrong with you code.

first of all, it's not a good conditional statement, since you are setting i to 2 and setting it to 24 afterwards, which is not what you want to do. you want to check if it is greater or lower than the other number. So the code between your parenthesis should be

for(var i = 2; i < 24; i+=2)

Secondly there is no code running since you don't have brackets after the loop it should be like this:

for(){
   console.log();
}

So if you combine these tips you should be able to complete challenge. Good luck

Pauline Hilborn
Pauline Hilborn
3,879 Points

Dumb question. How do you guys get snippets of your code to appear like that? I tried screen-shotting code, cut, then pasting into the question/comment field, but can't get it to accept an image.

thank you so much,in place of i<24; I put i<25; thanks:)