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 Hoddinott
Amber Hoddinott
6,494 Points

im completely lost

how do you make just odd numbers appear?

3 Answers

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

I think you are asking the wrong question. The number you are interested are called even numbers

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

Just in case odd number would be

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

How does this work? for loop can be verbally described as follow.

for (initialize; condition; change)

initialize: assign loop variable

condition: set stop condition

change: any changes you want to make for each iteration. This is typically used as a primary way to make your initial loop variable to reach stopping condition.

for (var i = 1; i <= 12; i++)
  console.log(2 * i);
Julian Aramburu
Julian Aramburu
11,368 Points

Or you could always take a shortcut and copy paste code :)

Julian Aramburu
Julian Aramburu
11,368 Points

Hi Amber! Are you referring to that particularly challenge? There you are asked to refactor that bunch of console.logs in order to do the same but with less code and you are hinted to use a loop to do so... so you could use a for loop maybe? and if you want to check if a number is even or not you could use modulus/reminder operator https://goo.gl/kab7qj!

If you are still lost after this, comeback for more help/hints!

Hope it helps you sort the problem out!

Cheers and Keep Coding!