-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
93 lines (79 loc) · 2.51 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
let cards = document.querySelectorAll('.card_inner')
let firstClick = false
let cardPair = []
cards.forEach((card)=>{
card.state = 'unclicked'
})
shuffle()
for(let i=0; i<cards.length; i++){
cards[i].addEventListener('click',()=>{
if(!firstClick){time()}
firstClick = true
if(cards[i].state == 'unclicked'){
cards[i].style.transform = 'rotateY(180deg)'
cards[i].state = 'clicked'
cardPair.push(cards[i])
check()
}
else if(cards[i].state == 'clicked'){
cards[i].style.transform = 'rotateY(0deg)'
cards[i].state = 'unclicked'
cardPair = []
}
})
}
function check(){
if(cardPair.length==2){
if(cardPair[0].querySelector('img').src==cardPair[1].querySelector('img').src){
matched()
}
else{
unmatched(cardPair[0],cardPair[1])
}
}
}
function matched(){
cardPair[0].state='blocked'
cardPair[1].state='blocked'
cardPair = []
let score = document.querySelector('#score').innerHTML
score++
document.querySelector('#score').innerHTML = score
}
function unmatched(x,y){
setTimeout(()=>{
x.state = 'unclicked'
y.state = 'unclicked'
x.style.transform = "rotateY(0deg)"
y.style.transform = "rotateY(0deg)"
},750)
cardPair[0].state = 'blocked'
cardPair[1].state = 'blocked'
cardPair = []
}
function time(){
let secs = 0
let mins = 0
let SS
let MM
setInterval(()=>{
secs++
if(secs==60){secs=0; mins++}
secs<10?SS=`0${secs}`:SS=`${secs}`
mins<10?MM=`0${mins}`:SS=`${mins}`
document.querySelector('#time').innerHTML = `${MM}:${SS}`
}, 1000)
}
function shuffle(){
let images = document.querySelectorAll('img')
let srcs = ['joker.jpg','arrow.png','batgirl.png','batman.png','captain.jpg','darkseid.png','doctor.jpg','flash.jpg','riddler.jpg','spiderman.jpg','superman.jpg','thor.jpg','joker.jpg','arrow.png','batgirl.png','batman.png','captain.jpg','darkseid.png','doctor.jpg','flash.jpg','riddler.jpg','spiderman.jpg','superman.jpg','thor.jpg']
for(let i=srcs.length-1; i>0; i--){
let j = Math.floor(Math.random() * i)
let temp = srcs[i]
srcs[i] = srcs[j]
srcs[j] = temp
}
for(let i=0; i<images.length; i++){
images[i].src = srcs[i]
}
}