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 trialAlex Lang
1,110 PointsWhat did I do wrong?
var html = '';
var red;
var green;
var blue;
var rgbColor;
function colors(red, green, blue) {
Math.floor(Math.random() * 256 );
}
for( var i = 1; i <= 10; i += 1 ) {
colors();
rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
html += '<div style="background-color:' + rgbColor + '"></div>';
}
document.write(html);
3 Answers
Antonio De Rose
20,885 Pointscan you please you the code formatter, and please use proper indentation, it is really hard to find, the issue.
how would the code, come in a nicer format, when you post a question is, like if the question comes from the background of javascript, see example
put 3 backticks, followed by the programming language, this case javascript
end with 3 backticks
document.write('hello');
click on the markdown cheatsheet link below, to get understanding.
Johnnt Trav
Courses Plus Student 1,295 Pointsvar html = '';
// red, green, and blue aren't being used so remove them
var red;
var green;
var blue;
var rgbColor;
function colors(red, green, blue) { //red, green, and blue are like variables that you can use inside your function scope. No where in your function are you using them, therefore you should remove them.
Math.floor(Math.random() * 256 ); // you need a return statement otherwise this function would give back nothing.
}
for( var i = 1; i <= 10; i += 1 ) {
colors(); // calling this function would give back nothing because you don't have a return statement. Even if you had a return statement this would output 1 random number, e.g. 145. So there's no reason you having that line of code there.
rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')'; // finally red, green, and blue should be replaced with your colors function.
html += '<div style="background-color:' + rgbColor + '"></div>';
}
document.write(html);
so it should look like this
var html = '';
var rgbColor;
function colors() {
return Math.floor(Math.random() * 256 );
}
for( var i = 1; i <= 10; i += 1 ) {
rgbColor = 'rgb(' + colors() + ',' + colors() + ',' + colors() + ')';
html += '<div style="background-color:' + rgbColor + '"></div>';
}
document.write(html);
Lucas Guimarães
Front End Web Development Techdegree Student 3,900 PointsI think there is one way much closer than yours
/// javascript
var html = ''; var red; var green; var blue; var rgbColor; function colors(red, green, blue) { red = Math.floor(Math.random() * 256 ); green = Math.floor(Math.random() * 256 ); blue = Math.floor(Math.random() * 256 ); return red, green, blue; }
for( var i = 1; i <= 10; i += 1 ) {
rgbColor = 'rgb(' + colors(red) + ',' + colors(green) + ',' + colors(blue) + ')'; html += '<div style="background-color:' + rgbColor + '"></div>';
} document.write(html); document.write("<h1> This is your last rgbColor " + rgbColor + "</h1>")
///
Rich Donnellan
Treehouse Moderator 27,696 PointsRich Donnellan
Treehouse Moderator 27,696 PointsQuestion updated with code formatting. Check out the Markdown Cheatsheet below the Add an Answer submission for syntax examples.