-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
141 lines (118 loc) · 3.34 KB
/
index.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
const cards = document.querySelectorAll('.memory-card');
let a, b
let score = 0
let round = 1
var open = true
var popup = document.getElementById('container')
const el = document.getElementById('title-card')
var display = document.querySelector('#time');
var duration = ""
var timer = duration, minutes, seconds;
init()
el.innerHTML = "Welcome to round " + round
function displayScore() {
var el = document.getElementById('congrats')
el.innerHTML = "<br><br>Your round " + round + "'s score is " + score + "<br><p>Round 2 started. 30 seconds only!</p>"
round = round + 1;
nextRound()
}
function nextRound() {
timer = duration, minutes, seconds;
cards.forEach(card => card.addEventListener('click', flipCard))
setTimeout(() => {
cards.forEach(card => card.classList.remove('flip'))
}, 200);
startTimer(display)
}
function toggleBtn() {
if(open===true) {
popup.classList.add('congratsOpen')
open = false
}
else {
popup.classList.remove('congratsOpen')
open = true
}
}
function init() {
var parent = document.querySelector(".memory-wrap");
for(var i = 0; i < parent.children.length; i++){
parent.appendChild(parent.children[Math.random() * i | 0]);
}
}
function flipCard() {
this.classList.toggle('flip')
if(firstCard === false) {
firstCard = true
a = this
}
else {
firstCard = false
b = this
matchCards(a, b)
}
}
function flipBack() {
this.classList.toggle('flipBack')
}
function matchCards(a, b) {
var myimg1 = a.getElementsByTagName('img')[0];
var mysrc1 = myimg1.src;
var myimg2 = b.getElementsByTagName('img')[0];
var mysrc2 = myimg2.src;
if(mysrc1 === mysrc2) {
a.removeEventListener('click', flipCard)
b.removeEventListener('click', flipCard)
score = score + 1;
el.innerHTML = "Score : " + score
if(score == 8) {
congragulate()
displayScore()
score = 8
round++;
}
}
else {
setTimeout(() => {
a.classList.remove('flip')
b.classList.remove('flip')
}, 600);
}
}
cards.forEach(card => card.addEventListener('click', flipCard))
let firstCard = false
const maxWidth = window.screen.width;
const maxHeight = window.screen.height;
function Random(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
function Shadows(amount){
let shadow = "";
for(let i = 0; i < amount; i++){
shadow += Random(0, maxWidth) + "px " + Random(0, maxHeight) + "px " + "rgb(255,"+ Random(0, 256) + "," + Random(0, 256) + "), ";
}
shadow += Random(0, maxWidth) + "px " + Random(0, maxHeight) + "px " + "rgb(255,"+ Random(0, 256) + "," + Random(0, 256) + ")";
return(shadow);
}
for(let i = 1; i <= 3; i++){
document.documentElement.style.setProperty('--shadows' + i, Shadows(100));
}
function congragulate() {
toggleBtn()
}
function startTimer(display) {
duration = 30
setInterval(function () {
console.log(minutes)
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
if (--timer < 0) {
timer = duration;
}
}, 1000);
}