From 067a5afcabbb30d96bd68dbe462b401850995322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CEva?= <“evacmak@outlook.pt> Date: Thu, 2 May 2024 21:58:40 +0100 Subject: [PATCH] Solved lab --- index.html | 7 ++++--- src/memory.js | 24 +++++++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/index.html b/index.html index 9386faaf4..893d72cee 100644 --- a/index.html +++ b/index.html @@ -5,7 +5,8 @@ Superhero Memory Game - + +
@@ -17,7 +18,7 @@

Score

Pairs guessed: 0

- - + + diff --git a/src/memory.js b/src/memory.js index f6644827e..830cdf51d 100644 --- a/src/memory.js +++ b/src/memory.js @@ -1,18 +1,32 @@ class MemoryGame { constructor(cards) { this.cards = cards; - // add the rest of the class properties here + this.pickedCards = []; + this.pairsClicked = 0; + this.pairsGuessed = 0; + this.shuffleCards(); } shuffleCards() { - // ... write your code here + if (!this.cards) return undefined; + + for (let i = this.cards.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [this.cards[i], this.cards[j]] = [this.cards[j], this.cards[i]]; + } } checkIfPair(card1, card2) { - // ... write your code here + this.pairsClicked++; + if (card1 === card2) { + this.pairsGuessed++; + return true; + } else { + return false; + } } checkIfFinished() { - // ... write your code here + return this.pairsGuessed === this.cards.length / 2; } -} +} \ No newline at end of file