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

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); }

6 Answers

Rachel Lev
Rachel Lev
14,579 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,306 Points

No one learns from the finished code.

Tadjiev Codes
Tadjiev Codes
9,622 Points

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

Adam Beer
Adam Beer
11,306 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.

Justyna Szofinska
Justyna Szofinska
6,174 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"

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

Hope this help Cheers!

Adam Beer
Adam Beer
11,306 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.

Celia Tawembe
Celia Tawembe
2,519 Points

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