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 trialdavid mchale
1,434 Pointsunexpected identifier
Somewhere in the html+= i have an unexpected identifier but not sure which it is...
var html = ''; var rgbColor;
function randomRGB(){ return Math.floor(Math.random() * 256); }
function randomColor(){ var color = 'rgb('; color += randomRGB() + ','; color += randomRGB() + ','; color += randomGRB() + ','; color += ')'; }
for(var i = 0; i < 10; i++){ rgbColor = randomColor(); html += '<div style="'background-color:' + rgbColor + '"></div>'; }
document.write(html);
2 Answers
Shawn Rieger
9,916 Pointsin your...
html += '<div style="'background-color:' + rgbColor + '"></div>';
Notice your closing your string too soon with the single quote after style="
. Technically speaking, your syntax error occurs because your string ends with no semi-colon, then you have background-color:
which is not a valid variable or method. Removing the extra single quote should solve your problem...
html += '<div style="background-color:' + rgbColor + '"></div>';
nico dev
20,364 PointsHi David,
Actually, there are a couple of quick fixes that might help you:
1) On the following lines:
function randomColor(){
var color = 'rgb(';
color += randomRGB() + ',';
color += randomRGB() + ',';
color += randomGRB() + ',';
color += ')';
}
the last RGB has a misspelling, so that should state RGB.
2) In the line:
for(var i = 0; i < 10; i++){
rgbColor = randomColor();
html += '<div style="'background-color:' + rgbColor + '"></div>';
}
You would ideally use only double-quotes before the background-color CSS property (i.e.: remove the single-quote before it).
That should hopefully help you get back on track. :)
nico dev
20,364 Pointsnico dev
20,364 PointsOops, sorry.
Exactly! :)
Shawn Rieger
9,916 PointsShawn Rieger
9,916 PointsDon't be sorry! Welcome to programming and making silly syntax errors all day long ;) It's a big part of being a programmer, looking for errors in your code.