I have following code snippest
How would you paint the cards(I need to paint 2 cards at same random color)
for (var i = 0; i < 50; ++i)
{
grid.innerHTML += '<div onclick="go(this)" class="card"></div> ';
}
const cards = document.querySelectorAll(".card");
const paintCards = () => {
for (var i = 0; i < cards.length; i += 2)
{
console.log(cards.style.backgroundColor)
cards.style.backgroundColor = generateRandomColor();
}
};
paintCards();
I did it with array of 25 random colors but I need to do it without it
2
Answers
In for loop, step of 2 is being used hence you may use
i
&i+1
inside the body of the loop.UPD: Added an array to keep the randomized colors in it. Once we empty it – let’s fill it again. I’ve used small pool size – e.g. 5 so we would get somehow randomized values. But this soultion is not the best – the distribution for elements that in border of beginning and end of the array would be different. But by this we would guarantee that cards would be colored in pairs. Also added initial painting for the main loop itself. Doing it as inline style. Hope that helps
Hey if I got your right – then you need to generate rnd color and keep it,
then paint two of cards into different color. Painting stuff depends on your req. Now I did paint of current and it’s neighbour,and then move to the next i+2. We could simply paint every odd or even, etc. Hope that helps