D.Sim D.Sim - 5 months ago 27
Javascript Question

Generating random colors in JavaScript

In the code below, the string 'rgb' means what? I don't understand why it's necessary.

function randomColors() {
var red = Math.floor(Math.random() * 256);
var green = Math.floor(Math.random() * 256);
var blue = Math.floor(Math.random() * 256);
var colors = 'rgb(' + red + ',' + green + ',' + blue + ')';
return colors;
}

Answer

It's the syntax of a CSS color:

.someClass { 
    background-color: rgb(255, 255, 255);
}

Look like your function is made to return a css color declaration.