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

Alina C
Alina C
1,553 Points

Hi, here's my solution. Feedback would be appreciated

let html = '';

function createColorPad(c=0, r=Math.floor(Math.random() * 256),g=Math.floor(Math.random() * 256),b=Math.floor(Math.random() * 256)) {
  return `<div style="background-color: rgb(${r}, ${g}, ${b})">${c}</div>`;
}

for (i=1; i<=10; i++) {
  html += createColorPad(i);
}

document.querySelector('main').innerHTML = html;

thanks for reading, happy coding

2 Answers

Steven Parker
Steven Parker
231,198 Points

Using parameters with default values is clever; but unless you're sure the function will need to be called with explicit arguments, a more conventional approach using assignments in the code body might produce easier-to-read code.

As it is, you could enhance readability by placing the complex argument expressions on separate lines:

function createColorPad(
  c = 0,
  r = Math.floor(Math.random() * 256),
  g = Math.floor(Math.random() * 256),
  b = Math.floor(Math.random() * 256)
) {

But kudos for creativity!   :+1:

Alina C
Alina C
1,553 Points

Thank you Steven, I really forgot to take readability into account 😔