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 Solution

Use of comma inside ' , ' Why the preview doesn't show if i remove it ??

var html = ' '; var red; var green; var blue; var rgbColor;

for ( var i = 1; i <= 100; i +=1) { red = Math.floor(Math.random() * 256 ); green = Math.floor(Math.random() * 256 ); blue = Math.floor(Math.random() * 256 ); rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')'; html += '<div style="background-color:' + rgbColor + '"></div>'; } document.write(html);

rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';

On the line of code above a coma is used inside ' , ' If i remove it, and try to preview the code nothing shows. I thought an empty space between ' ' was supposed to be space . For what does the comma stands for?

1 Answer

You are programmatically adding background-color (a CSS property) to a div element. You can define a background-color in several ways. A common way is with a hexadecimal value (e.g background-color: #ffab67). However, you can also define it with a decimal value with (e.g background-color: rgb(255,0,255);).

Here, the comma is used to separate the red, green and blue values from eachother. If you didn't need to do this, and you had rgb(2550255) that could be red:25, blue:50 and green:255 or red:255, blue:0 and green:255. So to make it clear, the commas are used to separate the different values.

So if you remove a comma, this becomes an invalid CSS property, and no color is added.

Thank you Christian. It was very clear.