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 trialKelly Clark
4,387 PointsWhy 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"?
var i = 0;
for ( var i = 4; i <=156; i =+ 1) {
console.log(i);
}
2 Answers
Erik McClintock
45,783 PointsKelly,
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
Radu Ioan Stochita
3,558 PointsThere is a mistake, you said:
i =+ 1
This is correct:
i += 1
P.S: You don't have to declare the variable outside.
Kelly Clark
4,387 PointsThank you!
Kelly Clark
4,387 PointsKelly Clark
4,387 PointsWow, can't believe I missed that, it was driving me crazy! Thank you!
Erik McClintock
45,783 PointsErik McClintock
45,783 PointsAbsolutely! 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