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

I think I did it right, but.....

I have to make a look to console.log 2 to 24 I did it? Heelp It sais I loogen in 23 but had to log in 12? i dont understand

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

3 Answers

jared eiseman
jared eiseman
29,023 Points

There's a couple of things here.

For starters, in the loop, (i = 2; i<24; i++), the last condition is shorthand for i = i + 1. You need your loop to count by even numbers, so a simple implementation of this would be to do i = i + 2, or i += 2 for short. (also, this will only count up to 24, not including 24)

Secondly, you're logging 2 each time. So with the correct loop you would log 2 out a bunch of times. Consider logging the variable instead.

Here is a working code sample:

for (var i = 2; i <= 24; i += 2) {
    console.log(i);
}
Austin Erb
Austin Erb
6,827 Points

You are close.

You start off with i = 2 which is good, but you are missing the 'var' keyword before i.

Also, your loop stops when i < 24 which is close, but because you use < instead of <=, your program will stop before it gets to 24.

And finally, you need to increment your i variable by += 2 instead of += 1.

I hope this explanation was understandable.

thanks, I dont know witch I would rate ghestr i'l take the first one...