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

The code below logs all of the even numbers from 2 to 24 to the JavaScript console. However, there's a lot of redundant

I've been at this, and it's killing me I can't figure it out.

script.js
for (  i = 2; i <= 25; 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>

I figured it out with the help of this MDN page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

This is what worked for me:

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

8 Answers

Rachel Lev
Rachel Lev
14,583 Points

You have to add 2 to i in order to get the even numbers, and i should be less or equal to 24 not 25:

for (i = 2; i <= 24; i += 2) {
console.log(i);
}
Adam Beer
Adam Beer
11,314 Points

No one learns from the finished code.

Tadjiev Codes
Tadjiev Codes
9,626 Points

Why do we need += in the end of the code within () ? Thanks

Justyna Szofinska
Justyna Szofinska
6,178 Points
for ( let i = 2; i <= 24; i += 2) {
console.log ( i );
}

You need to add variable 'let'. I also had problem to finished it because i didn't erase previous code (console.log(2) console.log(4)... ect)

Andrew Duty
Andrew Duty
4,856 Points

This is what got it for me, had everything right expect the "let"

Adam Beer
Adam Beer
11,314 Points

Challenge Task 1 of 1

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.

console.log(2);
console.log(4);
console.log(6);
console.log(8);
and so on...

And that's how your code work:

for (var i = 2; i <= 25; i++) { // In other words, i++ equal to i += 1
    console.log(i);

}

//the output
console.log(2);
console.log(3);
console.log(4);
console.log(5);

You are so close but it grows badly. This should be corrected and i <= 24. Hope this help.

Gheon Steenkamp
Gheon Steenkamp
458 Points

None of these codes work on this discussion

Kyffin Richmond
Kyffin Richmond
2,534 Points

Solved: The code uses a loop to display even numbers from 2 to 24. It starts at 2 and adds 2 repeatedly until it reaches 24, showing all the even numbers in between.

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

Hope this help Cheers!

Adam Beer
Adam Beer
11,314 Points

And again, No one learns from the finished code. Second, your first two I is capitalized, and the last is lowercase, why? Now your code doesn't work.

Here the problem you should clear the coding area first and then start writing your loop code.

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