// list of symbols
let cardsSymbols = ["fa fa-diamond", "fa fa-paper-plane-o", "fa fa-anchor", "fa fa-bolt", "fa fa-cube",
"fa fa-leaf", "fa fa-bicycle", "fa fa-bomb"
];
// Grab all the elements with class "card"
let elementsHavingClassCard = document.getElementsByClassName("card");
//console.log(cardClassElements);
// Add each element into an array
let cards = [...elementsHavingClassCard];
// An array that holds list of open cards
let openCards = [];
// deck of all cards
let deck = document.querySelector(".deck");
// Shuffle function from http://stackoverflow.com/a/2450976
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;
}
// Shuffle cards eveytime
function initializeGame() {
let shuffleCards = shuffle(cards);
for (let card = 0; card < shuffleCards.length; card++) {
shuffleCards.forEach.call(shuffleCards, function(item) {
deck.appendChild(item);
});
}
//}
} // func shuffleCards ends
// Run the function as soon as the page is loaded
window.onload = initializeGame();
// When a card is clicked
function cardClicked(i) {
return function() {
// Display the card symbol when the card is clicked
elementsHavingClassCard[i].classList.add("match");
// Run checkMatchedCard function
checkMatchedCard(i)
};
} // func cardClicked ends
// Add event listener to each card element
for (let i = 0; i < elementsHavingClassCard.length; i++) {
elementsHavingClassCard[i].addEventListener("click", cardClicked(i));
}
// check if two cards matches
function checkMatchedCard(i) {
// push the...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.