-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
103 lines (84 loc) · 2.92 KB
/
script.js
File metadata and controls
103 lines (84 loc) · 2.92 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
100
101
102
103
const gameContainer = document.getElementById("game");
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);
}
}
// TODO: Implement this function!
function handleCardClick(event) {
// you can use event.target to see which element was clicked
event.target.classList.toggle("flipped");
console.log(`# cards flipped: ${document.getElementsByClassName("flipped").length}`);
if((document.getElementsByClassName("flipped").length) == 2){
console.log('we have two cards flipped');
const elementArray = Array.from(document.getElementsByClassName("flipped"));
console.log(elementArray);
//check to see if the cards match
//if cards match, switch class to match and keep face up
if(checkIfMatch(elementArray)){
elementArray.forEach((element) => element.classList.add('match'));
setTimeout(() => elementArray.forEach((element) => element.classList.remove('flipped')), 1500);
console.log(Array.from(document.getElementsByClassName("match")).length);
if(Array.from(document.getElementsByClassName("match")).length == 10){
alert("YOU WON!!!");
}
}
//No match - remove 'flipped' class to turn the cards back over
else{
setTimeout(() => elementArray.forEach((element) => element.classList.remove('flipped')), 1500);
}
}
}
function checkIfMatch(elementArray){
if(elementArray[0].className == elementArray[1].className){
console.log("WE HAVE A MATCH!");
return true;
}
else{
console.log('bummer no match');
return false;
}
}
// when the DOM loads
createDivsForColors(shuffledColors);