-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathmemory.js
More file actions
39 lines (35 loc) · 1.03 KB
/
memory.js
File metadata and controls
39 lines (35 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class MemoryGame {
constructor(cards) {
this.cards = cards;
// add the rest of the class properties here
this.pickedCards = [];
this.pairClicked = 0;
this.pairGuessed = 0;
}
shuffleCards() {
// ... write your code here
for (let i = this.cards.length - 1; i >= 0; i--) {
const randomIndex = Math.floor(Math.random() * (i + 1));
this.cards.push(this.cards[randomIndex]);
this.cards.splice(randomIndex, 1);
}
}
checkIfPair(card1, card2) {
const card1Name = card1.getAttribute("data-card-name");
const card2Name = card2.getAttribute("data-card-name");
this.pairsClicked++;
document.querySelector("#pairs-clicked").innerHTML = this.pairsClicked;
if (card1Name === card2Name) {
this.pairsGuessed++;
document.querySelector("#pairs-guessed").innerHTML = this.pairsGuessed;
return true;
}
return false;
}
checkIfFinished() {
// ... write your code here
if (this.pairsGuessed === 12) {
console.log("Game is finished :)");
}
}
}