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 );

document.write(html);

Anything I should change? Thanks!

1 Answer

Steven Parker
Steven Parker
186,663 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.