-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
69 lines (52 loc) · 1.9 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
const cardsContainer = document.querySelector(".cards");
const colors =["crimson", "turquoise", "limegreen", "royalblue", "goldenrod", "magenta", "lavender", "coral"];
const colorsPicklist =[...colors, ...colors];
const cardCount=colorsPicklist.length;
let revealedCount=0;
let activeCard=null;
let awaitingEndOfMove = false;
function buildCard(color){
const element = document.createElement("div");
element.classList.add("card");
element.setAttribute("data-color", color);
element.setAttribute("data-revealed", "false");
element.addEventListener("click", ()=>{
const revealed = element.getAttribute("data-revealed");
if(awaitingEndOfMove || revealed==="true" || element===activeCard){
return;
}
element.style.backgroundColor = color; //inline css to individual card
if(!activeCard){
activeCard=element;
return;
}
const colorToMatch = activeCard.getAttribute("data-color");
if(colorToMatch===color){
activeCard.setAttribute("data-revealed", "true");
element.setAttribute("data-revealed","true");
activeCard=null;
awaitingEndOfMove=false;
revealedCount+=2;
if(revealedCount===cardCount){
alert("You win! Refresh to play again.");
}
return;
}
awaitingEndOfMove= true;
setTimeout(()=>{
element.style.backgroundColor=null;
activeCard.style.backgroundColor=null;
awaitingEndOfMove=false;
activeCard=null;
},1000)
});
return element;
}
//making cards
for(let i=0; i<cardCount; i++){
const randomIndex = Math.floor(Math.random()*colorsPicklist.length);
const color = colorsPicklist[randomIndex];
const card = buildCard(color);
colorsPicklist.splice(randomIndex, 1)
cardsContainer.appendChild(card);
}