-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathmemory.js
58 lines (50 loc) · 1.55 KB
/
memory.js
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
55
56
57
58
class MemoryGame {
constructor(cards) {
this.cards = cards;
this.pickedCards = []
this.pairsClicked = 0
this.pairsGuessed = 0
}
shuffleCards() {
if(this.cards == null) {
return undefined
} else {
for (let i = this.cards.length - 1; i > 0; i--) {
const copyOfItemPositionBeingLooped = this.cards[i];
const randomlySelectedPosition = Math.floor(Math.random() * i);
this.cards[i] = this.cards[randomlySelectedPosition];
this.cards[randomlySelectedPosition] = copyOfItemPositionBeingLooped
}
return this.cards
/* const shuffledCards = []
for (let i = this.cards.length - 1; i > 0; i--) {
let randomCard = this.cards[Math.floor(Math.random() * i)]
shuffledCards.push(randomCard)
this.cards.splice(this.cards.indexOf(randomCard), 1)
}
return shuffledCards */}
}
checkIfPair(card1, card2) {
console.log(card1, card2)
this.pairsClicked++
let clickDisplayed = document.getElementById("pairs-clicked")
clickDisplayed.innerHTML = this.pairsClicked
const pickedCard1 = card1.getAttribute("data-card-name")
const pickedCard2 = card2.getAttribute("data-card-name")
if (pickedCard1 === pickedCard2) {
this.pairsGuessed++
let guessDisplayed = document.getElementById("pairs-guessed")
guessDisplayed.innerHTML = this.pairsGuessed
return true
} else {
return false
}
}
checkIfFinished() {
if (this.pairsGuessed === this.cards.length/2) {
return true
} else {
return false
}
}
}