I am building a memory card game as my first javascript project. What I've done is made a section in HTML that contains 12 cards like this:
<section id="memorygame">
<div class="card">
<img class="front">
<img class="back" style="background-image: url(assets/img/banana.png);">
</div>
I then duplicated this via the cloneNode() Method so the grid would display 24 cards in total. Now I would like to use the following code to randomize the cards:
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
I assume I need to have an array to make it work, but how do I convert the cards into an array? I'm completely lost. If someone could explain how and why, I'd be very appreciative.