Workshop › Memory › Step 17 of 25

Pair or no pair

What we are doing

When the second card is open: the same fruit is a pair, both are matched and stay open. Different fruits must turn back over, but not at once, or the player never sees the second card. setTimeout waits 800 ms.

```js
  if (opened.length < 2) return

  const [a, b] = opened
  if (a.symbol === b.symbol) {
    a.matched = true
    b.matched = true
    opened = []
  } else {
    // Give the player time to see the second card before hiding both again.
    setTimeout(() => {
      a.faceUp = false
      b.faceUp = false
      opened = []
    }, 800)
  }
}
```

What it means

Your turn

At the end of flip, under opened.push(card), write the new lines, above the function's last }.

Skills: Game states, The game loop

All steps

  1. Get the canvas and the pen
  2. The table
  3. One card
  4. A row of cards
  5. Four rows
  6. Every fruit twice
  7. Cards that know themselves
  8. A card's corner
  9. Draw the cards from the list
  10. Draw again and again
  11. The face of a card
  12. A fair shuffle
  13. Which card is at a point?
  14. Click to turn a card
  15. One place for the rules
  16. Two cards at a time
  17. Pair or no pair
  18. Count the moves
  19. Show moves and pairs
  20. All pairs found
  21. Play again
  22. A number that follows
  23. Swap sides halfway
  24. Animate the turn
  25. Build it yourself: your best game