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 trialGena I
540 Pointsnot 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
for (var i=2; i<=24; i*2); {
console.log(i);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
5 Answers
Krishna Pratap Chouhan
15,203 Pointswhat 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);
}
andren
28,558 PointsThere are three issues:
i*2
does not actually change the value ofi
. It just calculates whati * 2
would be and then does nothing with the result. If you wanted to multiplyi
by two you would usei *= 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
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.
Gena I
540 Pointsi=i+2?
Gena I
540 Pointsi 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.
Gena I
540 Pointsok took out semicolon thanks
Krishna Pratap Chouhan
15,203 Pointsmy bad. good catch thanks.
Gena I
540 PointsGena I
540 Pointswhy is it i= 1+2?