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 trialJuan Gaitan V.
7,441 PointsHello, my code isn't running, though I believe it prints what you ask.
What do you think?
for (x = 0; x <= 24; ) {
console.log(++x % 2? '' : x);
}
<!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
1,101 PointsA 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).
Juan Gaitan V.
7,441 PointsJuan Gaitan V.
7,441 Pointsyou mean...
Trevor Skelton
1,101 PointsTrevor Skelton
1,101 PointsSo 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?