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

Max Bardus
Max Bardus
11,794 Points

What about my version?

var html = ''; var red; var green; var blue; var rgbColor;

function randomColors() { red = Math.floor(Math.random() * 256 ); green = Math.floor(Math.random() * 256 ); blue = Math.floor(Math.random() * 256 ); rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')'; return html += '<div style="background-color:' + rgbColor + '"></div>'; } var counter = 0; do { var colorRandom = randomColors(); counter++; } while (counter < 10); document.write(html);

1 Answer

Steven Parker
Steven Parker
174,057 Points

How so you see the div's with no dimensions? Or do you have some CSS to go with this?

If not, you could give the div's some height using inline styles:

  return (html += '<div style="background-color:' + rgbColor + ';height:2em"></div>');