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 Create a for Loop

Why does it keep saying my code took too long to run?

I'm trying to complete a challenge, it's asking me to create a for loop to log numbers to the console 4 through 156. Is what I have wrong? What does it mean that the code "took too long to run"?

script.js
var i = 0;
for ( var i = 4; i <=156; i =+ 1) {
  console.log(i);
}

2 Answers

Erik McClintock
Erik McClintock
45,783 Points

Kelly,

Your counter is not being incremented properly, thus you're creating an infinite loop here. If you look carefully, you'll see your += operator is backwards.

You have:

// notice, you have "i =+ 1"; this is backwards!
for ( var i = 4; i <=156; i =+ 1)

You need:

// notice now, it is "i += 1"; this is correctly incrementing your counter
for ( var i = 4; i <=156; i += 1)

With that one little fix, you'll be good to go!

Erik

Wow, can't believe I missed that, it was driving me crazy! Thank you!

Erik McClintock
Erik McClintock
45,783 Points

Absolutely! It's an easy mistake to make, and an even easier one to miss when you're trying to debug! Syntax and spelling errors are the worst!

Happy coding :)

Erik

There is a mistake, you said:

i =+ 1

This is correct:

i += 1

P.S: You don't have to declare the variable outside.

Thank you!