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 sure what im doing wrong here

for (var i=2; i<=24; i*2); { console.log(i); } it should console.log from 2 to 24 2 4 6 etc

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

5 Answers

what you are printing is:

console.log(2);
console.log(4);

console.log(8);

console.log(16);

console.log(32);

Spolier alert....

correct answer below

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

why is it i= 1+2?

andren
andren
28,558 Points

There are three issues:

  1. i*2 does not actually change the value of i. It just calculates what i * 2 would be and then does nothing with the result. If you wanted to multiply i by two you would use i *= 2.

  2. You should not actually be using multiplication. By multiplying i by two each time you will get a sequence like this: 2, 4, 8, 16. not 2, 4, 6, 8, 10, etc. You should be using addition instead which is done like this: i += 1

  3. You have a semicolon after the declaration of the loop, that should not be there. By placing it there you end the loop declaration prematurely.

If you fix those issues like this:

for (var i=2; i<=24; i+=2) { // Changed i*2 to i+=2 and removed semicolon
  console.log(i);
}

Then your code will pass.

i=i+2?

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

and i got it wrong and it said You logged 1 numbers to the console. You should log 12.

ok took out semicolon thanks

my bad. good catch thanks.