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

Bummer! Your code took too long to run.

Any ideas why this is happening?

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

2 Answers

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

You need to use var before the i in the first line of your code. You also have =+ instead of +=. Try this:

for (var i=2; i<=25; i+=2 ) {
  console.log(i);
}
Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi Samuel,

It might be because it's causing an infinite loop. You're counting in multiples of 2, so even numbers starting from 2. But your condition is an odd number (25). It never stops at 25 but would go onto 26. I might be wrong but i think that's what's causing it. :-)

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

The <= operator here would prevent that, since it should only run until i is less than or equal to 25. Once it hits 26 or above, the loop will terminate. :)

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

True enough :)

and it looks like the question has been solved (I missed the increment operator mistake - oops) so all is well. :)