-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
169 lines (141 loc) · 4.19 KB
/
script.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const gameContainer = document.getElementById("game");
const gameStartBtn = document.querySelector('button');
const restartGameBtn = document.querySelector('#Restart');
const scoreDisplay = document.getElementById('score');
restartGameBtn.style.display = 'none';
scoreDisplay.style.fontSize = '30px';
gameStartBtn.addEventListener('click',startPause)
restartGameBtn.addEventListener('click', restartGame);
const COLORS = [
"red",
"blue",
"green",
"orange",
"purple",
"red",
"blue",
"green",
"orange",
"purple"
];
// here is a helper function to shuffle an array
// it returns the same array with values shuffled
// it is based on an algorithm called Fisher Yates if you want ot research more
function shuffle(array) {
let counter = array.length;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
let index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
let temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
let shuffledColors = shuffle(COLORS);
// this function loops over the array of colors
// it creates a new div and gives it a class with the value of the color
// it also adds an event listener for a click for each card
function createDivsForColors(colorArray) {
for (let color of colorArray) {
// create a new div
const newDiv = document.createElement("div");
// give it a class attribute for the value we are looping over
newDiv.classList.add(color);
// call a function handleCardClick when a div is clicked on
newDiv.addEventListener("click", handleCardClick);
// append the div to the element with an id of game
gameContainer.append(newDiv);
}
}
let firstCard = null;
let secondCard = null;
let firstClick = false;
let isMatch = false;
let startGame = false;
let score = 0;
scoreDisplay.textContent = score;
// TODO: Implement this function!
function handleCardClick(event) {
// you can use event.target to see which element was clicked
if(startGame) {
isMatch = false;
let target = event.target;
if(!firstCard && !secondCard) {
target.style.backgroundColor = target.className;
firstCard = target;
firstClick = true;
setTimeout(() => {
if(isMatch) {
return;
}
firstClick = false;
firstCard.style.backgroundColor = '';
firstCard = null;
if(secondCard) {
secondCard.style.backgroundColor = '';
secondCard = null;
}
}, 1000);
} else if(firstClick){
firstClick = false;
secondCard = event.target;
secondCard.style.backgroundColor = target.className;
let card1stBg = firstCard.style.backgroundColor;
let card2ndBg = secondCard.style.backgroundColor;
if(card1stBg === card2ndBg && firstCard !== secondCard) {
updateScore();
isMatch = true;
removeCardEvent(firstCard, secondCard);
if(score === 5) {
restartGameBtn.style.display = 'inline-block';
setTimeout(alert, 100,'WON');
}
}
}
}
}
// helper functions
function resetCurrentCardFlip() {
firstCard = null;
secondCard = null;
firstClick = false;
}
function startPause() {
startGame = !startGame;
if(startGame) {
gameStartBtn.textContent = 'Pause';
} else {
gameStartBtn.textContent = 'Start Game'
}
}
function restartGame() {
let cards = gameContainer.children;
firstCard = null;
secondCard = null;
firstClick = false;
isMatch = false;
score = 0;
scoreDisplay.textContent = score;
for( let i = 0; i < cards.length; i++ ) {
cards[i].style.backgroundColor = '';
cards[i].addEventListener('click', handleCardClick)
}
}
function removeCardEvent(card1, card2) {
card1.removeEventListener('click', handleCardClick);
card2.removeEventListener('click', handleCardClick);
firstCard = null;
secondCard = null;
firstClick = false;
}
function updateScore() {
score++;
scoreDisplay.textContent = score;
}
// when the DOM loads
createDivsForColors(shuffledColors);