-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
101 lines (81 loc) · 2.88 KB
/
main.js
File metadata and controls
101 lines (81 loc) · 2.88 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
let moveCounter=0;
let matchCounter=0;
let hasFlippedCard=false;
let firstCard,secondCard;
let cards;
let srcArray=["img1.png","img2.png","img3.png","img4.png","img5.png","img6.png","img7.png","img8.png","img9.png","img10.png","img11.png","img12.png","img13.png","img14.png","img15.png","img1.png","img2.png","img3.png","img4.png","img5.png","img6.png","img7.png","img8.png","img9.png","img10.png","img11.png","img12.png","img13.png","img14.png","img15.png"];
const shuffleArray = array => {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
function flipCard(){
this.classList.add("flip");
moveCounter++;
updateScore();
if(!hasFlippedCard){
//first card click
hasFlippedCard=true;
firstCard=this;
firstCard.removeEventListener("click",flipCard);
} else{
//second card click
hasFlippedCard=false;
secondCard=this;
secondCard.removeEventListener("click",flipCard);
if(firstCard.children.item("image").src==secondCard.children.item("image").src&&!(firstCard==secondCard)){
//user made a match
matchCounter++;
updateScore();
firstCard.removeEventListener("click",flipCard);
secondCard.removeEventListener("click",flipCard);
} else{
//not a match
firstCard.addEventListener("click",flipCard);
secondCard.addEventListener("click",flipCard);
setTimeout(()=>{
firstCard.classList.remove("flip");
secondCard.classList.remove("flip");
},300);
}
}
}
function won(){
alert("You WON!!!!!");
}
function updateScore(){
let moves=document.getElementById("moves");
let match=document.getElementById("matches");
moves.innerHTML = moveCounter;
match.innerHTML = matchCounter;
if(matchCounter==15){
won();
}
}
function newGame(){
moveCounter=0;
matchCounter=0;
shuffleArray(srcArray);
for (let index = 0; index < cards.length; index++) {
const element = cards[index];
element.removeEventListener("click",flipCard);
element.classList.remove("flip");
element.addEventListener("click",flipCard);
element.children.item("image").src=srcArray[index];
}
updateScore();
}
function main(){
shuffleArray(srcArray);
let newGameBTNN=document.getElementById("newGameBtn");
newGameBTNN.addEventListener("click",newGame);
cards=document.querySelectorAll(".grid-item");
for (let index = 0; index < cards.length; index++) {
const element = cards[index];
element.children.item("image").src=srcArray[index];
element.addEventListener("click",flipCard);
}
}