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

This code doesn't look right. Any ideas?

So I'm working on this challenge, and this is where I've gotten so far. I don't think most of it is correct, so could someone give me a helpful hint.

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

2 Answers

You are REALLY close. Your forgot to put the code to decrease your counter each time though!

for (i = 2; i > 2; i -=) {
  console.log( i );
}

Wow! I have to say I'm a little proud of myself for getting that far! Thanks a ton!

Hmm. I wrote that code in my editor, but it still won't work.

but you still won't pass :) it is asking you print number from 2 to 24. so You are going to want to create a counter that equals 2, prints that number then increase the count by 2 then prints that number etc...

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

edit: had to correct a little typo in the code i+=2 not i+=24 :)

Ahh, I forgot about that part. I think I was thinking that I needed to just make an infinite evens number counter. Thanks again!

Do you understand what that for loop is doing? If not we can go though it step by step if you like.

No I think I understand it now! The console says I'm only logging 1 number, though. Guess I'll have to go through and look at the numbers more carefully.

Would you know why it's only logging a single number?

haha yeah I had a typo myself I set i+=24 instead of i+=2 so it was going through and printing the number 2 then it increased i by 24 so that made i=26 and popped out of the loop just printing one number :)

everyone typos :D

Great, I just figured it out! Thanks again!

AWESOME! sorry we had to get there the long way! haha

No problem!