-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathmemory.js
More file actions
54 lines (47 loc) · 1.3 KB
/
memory.js
File metadata and controls
54 lines (47 loc) · 1.3 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class MemoryGame {
constructor(cards) {
this.cards = this.shuffleCards(cards);
// add the rest of the class properties here
this.pickedCards = [];
this.pairsClicked = 0;
this.pairsGuessed = 0;
}
shuffleCards(arr) {
let j, temp;
if(!arr){
return undefined;
}
/* for (let i = mixedCards.length - 1; i > 0; i--){
j = Math.floor(Math.random()*(i+1));
temp = mixedCards[j];
mixedCards[j] = mixedCards[i];
mixedCards[i] = temp;
}
return mixedCards; */
let i = arr.length;
while (--i > 0) {
let randIndex = Math.floor(Math.random() * (i + 1));
[arr[randIndex], arr[i]] = [arr[i], arr[randIndex]];
}
return arr;
}
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 we have 12 pairs
if(this.pairsGuessed === 12){
console.log('Game is finished :)')
}
}
}