I have a site where I have 5 columns and I want to change their background with some random colors.
I don't get why sometimes it changes it, sometimes it doesn't and some columns remain the same as before. I've put an alert to check if "colors[i]" change, and it does. So why do some columns remain the same in some iteration and in others don't?
Please note that every column has the ".colorcolumn" class.
function calculation() {
$(".colorcolumn").each(function(i) {
let hexrandomCol = randomColor();
colors[i] = hexrandomCol;
$(this).css("background-color", colors[i]);
}
)}
// generate a random color
function randomColor() {
let color = "#";
while (color.length < 7) {
color += Math.floor(Math.random() * 256).toString(16);
}
return color;
}
// change colors on spacebar press
$("body").keyup(function (e) {
if (e.keyCode == 32) {
$(".save-palette").text("Save Palette");
calculation();
}
})