I want to do a button to shuffle the cards but it doesn't work how would you do it? I tried to shuffle the array but I need to shuffle the colors somehow (I need to stay with the same colors I created but in different order on the screen )
this is my code
const shuffle = deck => { /* code here */ }
document.addEventListener('DOMContentLoaded', () => {
function generateRandomColor() {
const hexValues = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F'];
let randomNum = "";
for (let i = 0; i < 6; i++) {
randomNum += hexValues[Math.floor(Math.random() * 16)];
}
return `#${randomNum}`
}
var grid = document.querySelector(".grid");
for (var i = 0; i < 50; ++i) {
grid.innerHTML += '<div class="card"></div>';
}
function generateArrayOfColors() {
var colors = [];
for (var i = 0; i < 25; i++) {
colors.push(generateRandomColor());
}
return colors;
}
var colorArray = generateArrayOfColors();
var cards = document.querySelectorAll(".card");
for (var i = 0; i < cards.length; ++i) {
cards[i].style.backgroundColor = colorArray[i % colorArray.length];
}
console.log(shuffle(cards))
const shuffleButton = document.querySelector(".shuffleButton");
shuffleButton.onclick = shuffle(cards);
});
<div class="grid"></div>
<button class="shuffleButton">Shuffle</button>