-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
120 lines (85 loc) · 3.09 KB
/
script.js
File metadata and controls
120 lines (85 loc) · 3.09 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
window.onload = pageLoad;
function pageLoad(){
// let startGame = array.from(document.getElementsByClassName("show-container"));
// let cards = array.from(document.getElementsByClassName("card"));
// startGame.forEach(startGame => {
// startGame.addEventListener('click', () => {
// startGame.classList.remove('visible');
// })
// })
// setTimeout(function() {
// document.getElementById("startGame")}, 5000);
// ;
let flipCard = document.querySelectorAll(".card");
let clickedCard = false;
let firstChoice, secondChoice;
let sameCard = 0;
let boxBoard = false;
flipCard.forEach(card => card.addEventListener('click', thisCard))
function thisCard(){
if (boxBoard) return;
this.classList.toggle('flip');
this.classList.add('flip');
//if statements
//when first card is clicked
if(!clickedCard){
clickedCard = true;
firstChoice = this;
console.log("First choice is " + firstChoice);
return;
}
//if firstChoice is opened, then secondcard is logic is
secondChoice = this;
clickedCard = false;
console.log("Second choice is" + secondChoice)
//call for matching
pairMatch();
}
//when firstChoice and secondChoice are matched,
function pairMatch() {
if (firstChoice.dataset.framework === secondChoice.dataset.framework)
{
disableCards();
return;
}
//then if they are not match, the animation is showing
flipbackCards();
}
function disableCards() {
firstChoice.removeEventListener('click', thisCard);
secondChoice.removeEventListener('click', thisCard);
}
function flipbackCards() {
boxBoard = true;
setTimeout(() => {
// call css (CSS Line 266 .card.incorrect) to make them vibrate
firstChoice.classList.add('incorrect');
secondChoice.classList.add('incorrect');
}, 500);
setTimeout(() => {
// call css
firstChoice.classList.remove('flip');
secondChoice.classList.remove('flip');
firstChoice = secondChoice = "";
clickedCard = false;
newGame();}, 1500);
}//end flipbackCards()
//source : https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
function randomShuffle(){
flipCard.forEach (card => {
let randomCard = Math.floor(Math.random() * 12);
card.style.order = randomCard;
});
}// end randomShuffle
//resets the game, and also shuffles the board
function newGame(){
[firstChoice, secondChoice] = [null, null];
[boxBoard, clickedCard] = [false, false];
} //end newGame()
//call randomShuffle
randomShuffle()
//if the person lost the game
// function youLose(){ }
flipCard.forEach(card=> {card.addEventListener("click", thisCard)})
} // end pageLoaded
//source: in HTML page