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

Andrew Nicholson
Andrew Nicholson
2,474 Points

I'm a bit lost as to how to make this log 12 times

Shouldn't the "i <= 24; i +=2" part make this log 12 times?

what have I missed?

Thanks. Beginning to think JS is beyond me.

script.js
for ( var i = 0; i <= 24; 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>

4 Answers

Syntax syntax syntax. Here's what it should be. In trade, can you tell me the difference between mine and yours?

for (var i=2; i<=24; i+=2){
  console.log(i)
};
Andrew Nicholson
Andrew Nicholson
2,474 Points

omg thanks so much - i knew i had it right, just couldn't work it out!!

(the semicolon was inside the curly braces)

Set your 'var i = 0;' to 'var i = 2;'. That should solve your problem. Just remember that the initial 'i' in the for loop is your beginning point, and will be counted as one of the loop rotations. I hope this makes sense! P.S. I put that first comment in the wrong section. My apologies!

Also, (if you haven't already) remember to remove all of the other 'console.log' functions written into the exercise before submitting your code. That's what burned me on that one :)

JS is fickle, but don't give up. You may need to learn a different way than how it is taught here. Look at some other resources that teach JS or have practice exercises for you to work with.

Andrew Nicholson
Andrew Nicholson
2,474 Points

Hi, thanks Ben and Brandon, I've changed the 0 to a 2 and this still says it only logs once. I think I initially did have the "2" as the inital "i" value, but changed it because it wouldn't work.

I might try restarting chrome and trying again.

Michael Oliver
Michael Oliver
7,855 Points

you will also want the last number to be 26 not 24.