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

Not quite sure where I'm going wrong

I need to console.log all even #'s from 2-24... could someone help me understand what I'm doing wrong here? Thank you! Code is as follows:

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

evens();

script.js
function evens() {
  for(i=2; i<=24; i += 2);
  console.log(i)
}

evens();
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

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Juliann,

You're on the right track, but the challenge wants you to write a for loop only (you also wrote a function). Just delete everything that is not directly part of the for loop and you're good to go.

Challenges are VERY specific and VERY picky. If you add something that isn't asked for, it will not pass you.

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

Welcome to Treehouse. Keep Coding! :)

Got it, thank you!

rdaniels
rdaniels
27,258 Points

This passed the challenge:

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

Hope this helps...