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,915 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>';
Shawn Rieger
Shawn Rieger
9,915 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 Trivelli
Nico Trivelli
20,354 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. :)