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

Hello, my code isn't running, though I believe it prints what you ask.

What do you think?

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

1 Answer

Trevor Skelton
Trevor Skelton
1,101 Points

A for loop requires the structure "for( initialize counter variable; when to stop counting; increment counter variable)". You can use the counter variable in your code inside the for loop. Switch your variable around and match the structure I described, e.g. for(int i=0;i<5;i++) { echo i; }. Also, you don't need all of the logic in the log statement. Think of a way to use your counter variable to go through just even numbers and just log(i).

you mean...

for (x = 1; x <= 24; x ++) {
  console.log(x % 2? '' : x);
}
Trevor Skelton
Trevor Skelton
1,101 Points

So now you have the syntax right. Your expression in the log parameter is correct, but I don't know if you can pass in an expression. Just do console.log(x). Think of a way to use your x as counting every even number in your for loop header. In other words, right now it is counting by adding 1 every time. What could you make it do instead of increment by one?