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

Akeia Escobar
Akeia Escobar
1,957 Points

I got my code to work, but how is it's readability?

var html = ''; var color; var rgbColor;

var colorCounter = 0; var color = function getColors(upper) { return Math.floor(Math.random() * upper); }

do { rgbColor = 'rgb(' + color(256) + ',' + color(256) + ',' + color(256) + ')'; html += '<div style="background-color:' + rgbColor + '"></div>'; colorCounter += 1; } while ( colorCounter <=10 );


Anything I should change? Thanks!

1 Answer

Steven Parker
Steven Parker
216,895 Points

You could give your function the name you will call it by and skip the assignment:

function color(upper) { return Math.floor(Math.random() * upper); }

OR you could assign it using the compact "arrow" function syntax:

var color = upper => Math.floor(Math.random() * upper);

And when posting code, use Markdown formatting (as I did here) to preserve the code appearance.