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
Rafael Freitas
2,515 PointsWhy syntax error for my solution?
This is my solution for the second part of the challenge. Can anyone help me understand why I get a syntax error a missing ; in the third getRandomNumber function call in my for loop? Why do the other 2 function calls not require a ;? Thanks so much!
var html = '';
var rgbColor;
for (var i = 1; i <= 10; i++) {
console.log(rgbColor);
rgbColor = 'rgb(' + getRandomNumber() + ',' + getRandomNumber() + ',' + getRandomNumber() ')';
html += '<div style="background-color:' + rgbColor + '"></div>';
}
function getRandomNumber() {
return Math.floor(Math.random() * 256 );
}
document.write(html);
3 Answers
Marcus Parsons
15,719 PointsHey Rafael,
Your syntax error comes from the line with rgbColor variable. You are missing a + operand right before the ')' string. Also, if you want the html to show up on the page, you need to add it to an element like so with some defined properties:
var html = ''; var rgbColor;
for (var i = 1; i <= 10; i++) {
rgbColor = 'rgb(' + getRandomNumber() + ',' + getRandomNumber() + ',' + getRandomNumber() + ')';
console.log(rgbColor);
html += '<div style="background-color:' + rgbColor + ';width:100px;height:50px;"></div>';
}
function getRandomNumber() {
return Math.floor(Math.random() * 256 );
}
document.write(html);
Rafael Freitas
2,515 PointsThanks Marcus. How do I get my code to show up on forum posts like you just did?
Marcus Parsons
15,719 PointsNo problem, Rafael! When you're posting code, follow what's in the Markdown Cheatsheet. Here is handy gif I have, as well.
If you're working on this code in Workspaces, the easiest and best way to share all of your code is to share a snapshot of your Workspace. The snapshot button is located in the top right corner of your Workspace window and will generate a URL for you to share on the forums. This is the very best way to share code as there is no chance of data being lost upon copy and pasting.
Rafael Freitas
2,515 PointsThanks again Marcus!
Marcus Parsons
15,719 PointsNo problem! If you want to go ahead and mark my answer as best answer, we can go ahead and close this question, but if you have any more questions, you can post back here, and I'll see it and respond. :)
