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!

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 The Refactor Challenge, Part 2

Alex Franklin
Alex Franklin
12,398 Points

This code is freezing and then crashing my browser every single time...

I work in another text editor so that I can save each lesson to look back on for help. This code causes my browser / text editor to freeze immediately upon load, and then soon after will crash and close out... I have noticed this happening from time to time, but this is the first time I can't even interact with the page b/c it's frozen upon load...

Can someone please explain why this happens, what is wrong with my code, and how to avoid this happening going forward? Thanks!


var html = ' '; var rgbColor;

function randomRGB() { return Math.floor(Math.random() * 256 ); }

function randomColor() { var color = "rgb("; color += randomRGB() + ','; color += randomRGB() + ','; color += randomRGB() + ')'; return color; }

function print(message) { document.write(message); }

for ( var i = 0; i , 100; i+= 1) { rgbColor = randomColor(); html += '<div style="background-color:' + rgbColor + '"></div>'; }



1 Answer

Jeff Muday
Jeff Muday
Treehouse Moderator 28,356 Points

I am not the best person to consult on JavaScript questions, but I think the problem is with your loop condition.

It should be

for (var i=0; i<100; i+= 1) 
Alex Franklin
Alex Franklin
12,398 Points

Wow such a simple mistake can really do some damage... THANK YOU JEFF!