Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops The Refactor Challenge, Part 2

Alex Franklin
Alex Franklin
12,397 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 27,547 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,397 Points

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