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

Kelly Miller
Kelly Miller
1,773 Points

Refactor challenge: why is my code not working?

Hello hive:

I haven't had any issues yet in refactoring code in the challenges presented, but now I'm stumped. To simply a console.log() statement that prints even numbers a total of 12 times, hence up to 24, I created the following loop:

for ( var i = 0; i >= 24; i += 2 ) { console.log(i); }

This answer is not accepted; the app says it's not printing 12 times to the console. I've also tried i >= 12, which didn't make sense to be, but I tried it anyway. Would appreciate your thoughts!

2 Answers

Kieran Barker
Kieran Barker
15,003 Points

Hi, Kelly! This is what the challenge is looking for:

for (var i = 2; i <= 24; i += 2) {
  console.log(i);
}

You need to begin at 2 and keep looping through as long as i is less than or equal to 24. I hope this helps!

Kelly Miller
Kelly Miller
1,773 Points

Thanks for your response. This helps. However, if i were to start at 0, could I simply set i <= to 22, and the answer would still be theoretically correct? Just curious.

Kieran Barker
Kieran Barker
15,003 Points

Hi Kelly,

Nope, that would print out 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22.