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 JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops The Refactor Challenge, Part 2

david mchale
david mchale
1,434 Points

unexpected 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
Shawn Rieger
9,916 Points

in 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
nico dev
20,364 Points

Oops, sorry.

Exactly! :)

Shawn Rieger
Shawn Rieger
9,916 Points

Don'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.

nico dev
nico dev
20,364 Points

Hi 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. :)