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 trialBakari Lewis
6,556 PointsNeed help...Why is it saying "Your code took too long to run"
Not sure how to make this code run faster. Or are there any errors in this code?
Thanks for the help :)
var count = 0;
while (count<27){
var count =+ 1;
document.write(count);
}
2 Answers
Steve Hunter
57,712 PointsI think you've redefined the variable count
by calling it var
a second time. I also think that =+
isn't incrementing your variable so the loop is carrying on forever as it'll never reach the escape value.
Have a look at this and see if it makes sense:
var count = 0;
while (count < 26) {
document.write(count);
count++;
}
I hope that helps.
Steve.
Nikhil Deorkar
Courses Plus Student 1,695 PointsCount need to be incremented using += or ++ not by using =+. When it says that it is taking to long to run, it usually means that your loop is incomplete, meaning that it is not being changed in any way, or that it has some sort of syntax error causing it to be an infinite loop going on forever and it could eventually crash your browser. Hope this helps!
Quick code example:
var count = 0;
while (count<27){
var count += 1; //you can also use 'count++;' to increment it by one
document.write(count);
}
Bakari Lewis
6,556 PointsThat makes sense now! Thanks a lot. :)
Nikhil Deorkar
Courses Plus Student 1,695 PointsGlad to help!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsI think in your code, the second definition of
var count
is out of scope to thewhile
control loop. So, your firstcount
variable, defined outside of the loop, stays at zero whilst the one inside does whatever it does but isn't controlling the loop. You've redefined a new variable inside the loop that's getting=+
applied to it - and I'm not sure=+
right - but the scope issue looks to be the reason the code will run forever - there's nothing incrementing the firstcount
.Steve.
Bakari Lewis
6,556 PointsBakari Lewis
6,556 PointsThanks! this worked!