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

Diego Salas Polar
Diego Salas Polar
21,431 Points

I need another solution to the code I've built.

Dave McFarland, I made a JavaScript program that generates different colors in each rectangle without refreshing the page. Hint: I use Ajax. The code in app.js isn't good and I need your help to refactor it so that my code can be clean. One more thing, I named the variables poorly, and I just wanted you to know that. Here is the link to my program: https://w.trhou.se/iwdgwtjafs.

Happy Coding, Diego Salas

1 Answer

Steven Parker
Steven Parker
229,732 Points

Here's a few suggestions.

While you're waiting for Dave, I was wondering what role AJAX performs here. It doesn't look like you get any new data so it seems like you could refactor the program to work without it.

Also, it looks like every call to randomRGB creates a new interval that calls itself when it fires, so in just a few seconds there would be an exponential increase in active intervals. You might want to reconsider your intended functionality, and I'd bet you can do what you want with just one interval (or a small fixed number) and no recursion.

Also, you have some rendering HTML in your head area. That h1 tag should be moved into the body.