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 trialcollinp1234567
255 PointsCould someone explain why this code doesn't work?
var html = '';
var red = Math.floor(Math.random() * 256 );
var green = Math.floor(Math.random() * 256 );
var blue = Math.floor(Math.random() * 256 );
var rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
for (var i = 0; i < 10; i++) { html += '<div style="background-color:' + rgbColor + '"></div>'; }
document.write(html);
This code produces 10 dots of the same colour, I'm struggling to understand why this is not a valid solution : ( Could someone break it down for me Also i'm sorry i don't know how to include a snapshot of the code instead of writing it in :(((
Elijah Quesada
Front End Web Development Techdegree Graduate 32,965 PointsThe colors are the same because the red, green and blue variables produce only one value when the script runs.
1 Answer
Steven Parker
231,275 PointsThis code creates a random color, but only once. Then the loop creates 10 elements of that same color.
If you want each element to have a different color, you'd need to put put the part of the code that picks the color inside the loop (where "red", "blue", "green", and "rgbColor" get assigned).
KRIS NIKOLAISEN
54,972 PointsKRIS NIKOLAISEN
54,972 PointsTo take a snapshot click the camera icon in the upper right corner of the workspace. This will create a link that you can then copy/paste.
What are you trying to do with the dots? You want them to be different random colors?