Open
Description
I discovered a bug where, if the user rapidly clicks on multiple cards, they remain flipped even though no match has actually occurred. However, this issue can be resolved with a simple adjustment in the handleCardClick function. Here’s the updated code snippet:
function handleCardClick(card: Card) {
if (card.isFlipped || card.isMatched) return;
// this line
const isFlippedMaximumCardsAtSameTime = cards.filter(card =>
card.isFlipped && !card.isMatched).length === 2;
if (isFlippedMaximumCardsAtSameTime) return;
setCards((prevCards) => {
const newCards = [...prevCards];
newCards[card.id].isFlipped = true;
const flippedCards = newCards.filter(
(c) => c.isFlipped && !c.isMatched,
);
if (flippedCards.length === 2) {
handleCardMatch(flippedCards[0], flippedCards[1]);
}
return newCards;
});
}
Metadata
Metadata
Assignees
Labels
No labels