I have 6 squares and I want to assign them 6 already defined colors, but randomly. So there should be 1 color for 1 square, and every time I click "play again" they should just mix randomly. So far I've managed to make all the squares to use one of the six colors, but as they choose them only randomly it happens pretty often that I have for example 3 gray colors, but 0 red. I am not sure how to explain this better, so if you have any questions I'll gladly answer them.
HTML:
<div id="container">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
JS:
var squares = $(".square");
var colors = {
'yellow': 'rgb(255, 255, 0)',
'orange': 'rgb(255, 153, 0)',
'green': 'rgb(0, 255, 0)',
'blue': 'rgb(0, 0, 255)',
'gray': 'rgb(128, 128, 128)',
'purple': 'rgb(153, 0, 255)'
};
for(var i=0; i < squares.length; i++) {
squares[i].style.backgroundColor = randomColors();
squares[i].style.display = "block";
}
function randomColors(ranCol) {
var ranCol = Object.keys(colors)[Math.floor(Math.random()*Object.keys(colors).length)];
return ranCol;
}