Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,507 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.