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

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,028 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,028 Points

Hi Kelly,

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