-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.js
More file actions
99 lines (90 loc) · 2.28 KB
/
Copy pathtemp.js
File metadata and controls
99 lines (90 loc) · 2.28 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//Page vars
this.resetBtn = document.querySelector("#reset");
//Game vars
let points = 0;
let firstCard = -1;
let numPairs = Math.floor(cardDivs.length / 2);
let maxPoints = numPairs;
//Initial array containing all the pairs of cards
let cards = [];
let cardsPicked = [];
//Helper functions
const shuffle = (array) => {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
};
const isMatch = () => {
if (cardsPicked.length < 2) {
return false;
} else {
if (cardsPicked[0].id == cardsPicked[1].id) {
return true;
} else {
return false;
}
}
};
const rmPair = () => {
//Cards matched. Remove from game
console.log("Correct guess!.");
//Remove id
cardsPicked[0].id = "";
cardsPicked[1].id = "";
};
const flipPair = () => {
//Clear content
cardsPicked[0].innerHTML = "";
cardsPicked[1].innerHTML = "";
//Remove class of clicked
cardsPicked[0].classList.remove("clicked");
cardsPicked[1].classList.remove("clicked");
};
const clearCardsPicked = () => {
while (cardsPicked.length > 0) {
cardsPicked.pop();
}
};
const resetGame = () => {
//Assign id's to html elements
shuffle(cards);
let index = 0;
if (cardDivs.length == cards.length) {
for (cardDiv of cardDivs) {
cardDiv.classList.remove("clicked");
cardDiv.id = cards[index];
cardDiv.innerHTML = "";
// cardDiv.innerHTML = cards[index];
index++;
}
}
points = 0;
cardsPicked = [];
score.innerHTML = `Score: ${points}`;
message.classList.remove("win");
};
const handleFirstCard = (e) => {
console.log("Picking first card!");
//Change card background and show contents
let firstCard = e.target;
firstCard.classList.add("clicked");
firstCard.innerHTML = firstCard.id;
cardsPicked.push(firstCard);
console.log("Pushed card", firstCard);
};
const handleSecondCard = (e) => {
console.log("Picked second card");
let secondCard = e.target;
//Don't let player pick the same card
if (secondCard.classList[1]) {
console.log("Cant pick same card!");
return;
} else {
cardsPicked.push(secondCard);
}
//Change card background
cardsPicked[1].classList.add("clicked");
//Show contents
cardsPicked[1].innerHTML = cardsPicked[1].id;
};