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

Log an even number from 2 to 24 using loop.

The code below logs all of the even numbers from 2 to 24 to the JavaScript console. However, there's a lot of redundant code here. Re-write this using a loop. I can't get the number 12 on the console log. What am I doing wrong with this challenge?

script.js
for (var i = 2; i < 15; i++){
  console.log(i);
}
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

Use 'let' to remove the scope from global

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

15 Answers

You need to create a for loop that initializes the variable to 2, console.log the variable to the screen, and add two to the variable on each iteration.

Said another way: starting at two, as long as i is less than 25, add two and log the new variable to the screen.

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

Why don't you put in the variable everytime like they do in the videos??

I did this exact code and it didn't work.

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

This code worked fine for me

Wouldn't

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

work just as well instead of i < 25?

heeeeeellllllllllo sister!

no it's cause you need to add the number 25 like this:

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

Are you actually sisters?

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

This works perfectly. Thanks, Chris Drew. Think this particular challenge was a little buggy.

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

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

very easy

for ( var i = 2 ; i <= 24 ; i++ ) { if ( i % 2 === 0 ) { console.log (i);

}

}

sorry but this code doesn't work , the right code is like this :

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

Thanks a lot.

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

Most of you are correct, but you're missing the let statement for the 'i'.....

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

Joseph yours works too, I think the %2 was mentioned in mdn

var eveni= [1, 2, 3, 4, 5, 6, 8, 9, 10]; for(i=0; i<eveni.length; i+=2){ if(i % 2===0){ console.log("even",i); }}

for ( var i = 2 ; i <= 24 ; i++ ) { if ( i % 2 === 0 ) { console.log (i);

}

}

var number = 0;

do{ number += 2; console.log(number);

}while(number< 24);

Hi everyone,

I'm trying to submit this answer and I'm not sure if its being buggy or not. Could someone help me out?

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