Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flip multiplos cards at same time in - [solving react interview questions] #8

Open
pedrolgcs opened this issue Jan 26, 2025 · 0 comments

Comments

@pedrolgcs
Copy link

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;
   });
 }
@pedrolgcs pedrolgcs changed the title Flip multiplos cards ate same time in - [solving react interview questions] Flip multiplos cards at same time in - [solving react interview questions] Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant