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

the example don't work

let html = ''; const randomValue = () => Math.floor(Math.random() * 256);

function randomRGB() { const color = rgb( ${randomValue()}, ${randomValue()}, ${randomValue()} ); return color;
}

for (let i=1; i<=64; i++) { html += <div style="background-color: ${randomRGB()}">${i}</div>;
}

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

Cameron Childres
Cameron Childres
11,818 Points

Hi Martín,

If you click on "markdown cheatsheet" below the comment box you'll find ways to format your code so we can read it easier, otherwise characters might be dropped like your backticks on the template literal. Here is your code with formatting:

let html = ''; 
const randomValue = () => Math.floor(Math.random() * 256);

function randomRGB() { 
  const color = `rgb( ${randomValue()}, ${randomValue()}, ${randomValue()} )`; 
  return color;
}

for (let i=1; i<=64; i++) { 
  html += `<div style="background-color: ${randomRGB()}">${i}</div>`;
}

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

This code runs for me just fine, producing 64 numbered divs with randomized background colors on each refresh.

If you let us know more details about the issue you're having that would help. Are you getting any console errors? Is anything happening at all? Also worth checking that your HTML has a <main> element and that the script source is loaded after it.

1 Answer

Steven Parker
Steven Parker
231,007 Points

Assuming your code contains the backtick characters where shown in Cameron's example, the code itself seems to be good.

However, for this code to work there must also be an HTML code component that contains a "<main>" element.