A memory card game is one of the best first projects for a JavaScript beginner. The rules are simple — flip two cards, find the matching pair — but building it teaches you shuffling, DOM manipulation, state tracking, and timers. By the end you have a real game you can share with anyone.
What you are building
A 4x4 grid of 16 cards (8 pairs). Click a card to flip it. Click a second card: if they match, they stay face up; if not, they flip back after a short delay. A move counter and a timer track your performance, and a message appears when all pairs are found.
The HTML structure
Each card is a div with a front (the hidden symbol) and a back (the card cover). Keep it simple:
<div class="board" id="board"></div>
<p>Moves: <span id="moves">0</span></p>
<p>Time: <span id="time">0</span>s</p>
<button id="restart">Restart</button>
Generate the cards from JavaScript instead of writing 16 divs by hand. That way the shuffle is easy too.
Shuffling the deck
Put your 8 symbols in an array, duplicate it, and shuffle with Fisher-Yates:
const symbols = ["🍎", "🚀", "🎵", "⚽", "🌙", "🐱", "🚗", "📚"];
const deck = [...symbols, ...symbols];
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
The matching logic
Track two flipped cards. Lock the board while a pair is being checked so fast clicks cannot break the game:
let firstCard = null;
let secondCard = null;
let lockBoard = false;
let moves = 0;
function flipCard(card) {
if (lockBoard || card === firstCard) return;
card.classList.add("flipped");
if (!firstCard) {
firstCard = card;
return;
}
secondCard = card;
moves++;
document.getElementById("moves").textContent = moves;
if (firstCard.dataset.symbol === secondCard.dataset.symbol) {
firstCard = null;
secondCard = null;
checkWin();
} else {
lockBoard = true;
setTimeout(() => {
firstCard.classList.remove("flipped");
secondCard.classList.remove("flipped");
firstCard = null;
secondCard = null;
lockBoard = false;
}, 800);
}
}
The flip animation
Use CSS 3D transforms so the card actually flips instead of just appearing:
.card {
transform-style: preserve-3d;
transition: transform 0.5s;
}
.card.flipped {
transform: rotateY(180deg);
}
What to add next
Once the basic game works, try a difficulty selector (4x3 for easy, 4x4 for hard), a best-score saved in localStorage, or a small sound when a pair matches. Each one is a small, self-contained feature — a good way to keep practising without rewriting the game.
Top comments (1)
Arhan, this is a clean first-project pick — a 4x4 grid with 16 cards and 8 pairs teaches shuffling, DOM manipulation, state tracking and timers in one small game, and the reader ends up with something they can actually share. The detail that tells me you built this for real is lockBoard: fast clicks breaking the pair check is the bug every beginner hits, and naming it before they find it the hard way is exactly what a good tutorial does. Fisher-Yates on the duplicated 8-symbol array is the right call too — no hand-written 16 divs, no rigged deck. One question: for the difficulty selector, would you change the grid to 4x3 for easy and keep the same 0.5s flip, or would you slow the flip down for younger players so the second card stays visible a beat longer?