This repository was archived by the owner on Sep 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (67 loc) · 2.37 KB
/
Copy pathscript.js
File metadata and controls
82 lines (67 loc) · 2.37 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
let cards = document.querySelectorAll('.card');
let nCards = cards.length;
cards.forEach(card => card.addEventListener('click', flipCard));
if(!localStorage.getItem('highscore')) localStorage.setItem('highscore', Infinity);
//put highscore in the html
document.getElementById('highscore').innerHTML = localStorage.getItem('highscore');
(function reorderCards(){
cards.forEach(card => {
card.style.order = Math.floor((nCards - 1)*Math.random());
});
})();
let [flipped, lockBoard, first, second, currentScore, found] = [false, false, undefined, undefined, 0, 0];
function flipCard(){
if(lockBoard) return "locked the board";
if(this === first) return "can't match first card with its self";
this.classList.toggle('flipped');
if(!flipped){//first flip
flipped = true;
first = this;
}
else {//second flip
flipped = false;
second = this;
lockBoard = true;
if(first.dataset.val === second.dataset.val){
setTimeout(() => {
first.removeEventListener('click', flipCard);
second.removeEventListener('click', flipCard);
lockBoard = false;
[first, second] = [undefined, undefined];
}, 1000);
found += 2;
}
else {
setTimeout(() => {
first.classList.remove('flipped');
second.classList.remove('flipped');
lockBoard = false;
[first, second] = [undefined, undefined];
}, 1000);
}
}
updateCurrentScore();
//check if game ends, if so: add return button to bottom of screen & update highscore if need be
endGame();
}
function updateCurrentScore(){
currentScore ++;
document.getElementById('currentscore').innerHTML = currentScore;
}
function endGame(){
if(found === nCards){
appendReturnButton();
//check if currentscore < highscore
if(currentScore < +localStorage.getItem('highscore')){
localStorage.setItem('highscore', currentScore);
}
}
}
function appendReturnButton(){
let a = document.createElement("a");
a.href = "land.html";
let button = document.createElement("button");
button.innerText = "RETURN!";
a.appendChild(button);
document.getElementsByClassName('returnbutton-container')[0].appendChild(a);
}