This is the code that I wrote, I dont know why all the 50 rgbColor generate the same value even when I wrote the random function in randomRGBValue.
var html = '';
var valueArr = [];
var rgbColor;
function randomValue() {
for (var i = 0; i < 3; i += 1) {
valueArr.push(Math.floor(Math.random() * 256 ));
}
return valueArr;
}
function randomRGB() {
randomValue();
var color;
color = 'rgb(';
color += valueArr[0] + ',';
color += valueArr[1] + ',';
color += valueArr[2] + ')';
return color;
}
for (var i = 0; i < 50; i += 1) {
rgbColor = randomRGB();
html += '<div style="background-color:' + rgbColor + '"></div>';
}
document.write(html);
To ease understanding of my poor written code,
-> randomValue function is for generating random value from 1 to 255(3 sets) and is pushed into valueArr. valueArr is returned.
Example valueArr = (value1, value2, value3)
-> randomRGB function calls randomValue function and plugs in valueArr value into the color array.
Aucun commentaire:
Enregistrer un commentaire