Skip to content

Commit 8e82c87

Browse files
committed
Added Alert Message & Style. #Day16
1 parent c9e9824 commit 8e82c87

File tree

4 files changed

+34
-30
lines changed

4 files changed

+34
-30
lines changed

Diff for: memory-game/README.md

-26
This file was deleted.

Diff for: memory-game/app.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ const cardArray = [
4848
img: 'images/backbone.png'
4949
}
5050
]
51-
51+
cardArray.sort(() => 0.5 - Math.random());
5252
let selectedCards = [], selectedCardIds = [], cardsWon = [];
5353
const grid = document.querySelector(".grid");
5454
const result = document.getElementById("result");
55+
const alert = document.querySelector(".alert");
5556

5657
window.addEventListener("DOMContentLoaded", () => {
5758
createBoard();
@@ -106,6 +107,7 @@ checkForMatch = () => {
106107
img.removeEventListener("click", flipCard);
107108
}
108109
}
110+
setAlert("Card Matched!", "success");
109111
}
110112
// CASE 3: ON INCORRECT MATCH OF CARDS
111113
else {
@@ -118,7 +120,8 @@ checkForMatch = () => {
118120

119121
// on completion of game
120122
if (cardsWon.length === cardArray.length / 2) {
121-
// alert
123+
setAlert("Congratulations! You have won the game!", "success");
124+
result.textContent = cardsWon.length + "! You have won the game!";
122125
}
123126
}
124127

@@ -129,4 +132,15 @@ setLogo = (id0, id1) => {
129132
imgs[id0].setAttribute("src", "images/logo.png");
130133
imgs[id1].setAttribute("src", "images/logo.png");
131134
}, 1000);
135+
setAlert("Card does not match!", "danger");
132136
}
137+
138+
// method to display alert message
139+
setAlert = (text, styleClass) => {
140+
alert.textContent = text;
141+
alert.classList.add(`alert-${styleClass}`);
142+
setTimeout(() => {
143+
alert.textContent = ``;
144+
alert.classList.remove(`alert-${styleClass}`);
145+
}, 1000);
146+
}

Diff for: memory-game/index.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
</head>
1010
<body>
1111
<div class="grid"></div>
12-
<h3>Your Score : <span id="result"></span></h3>
12+
<p class="alert"></p>
13+
<h3>Your Score : <span id="result">0</span></h3>
1314
<!-- javascript -->
1415
<script src="app.js"></script>
1516
</body>

Diff for: memory-game/style.css

+16-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,25 @@
66
padding: 50px;
77
}
88

9-
h3 {
9+
h3, p {
1010
padding-left: 50px;
11+
text-transform: capitalize;
1112
}
1213

1314
body {
1415
background-color:#FCFCC9;
16+
}
17+
18+
.alert {
19+
margin-bottom: 1rem;
20+
height: 1.25rem;
21+
border-radius: 0.25rem;
22+
}
23+
.alert-danger {
24+
color: #721c24;
25+
background: #f8d7da;
26+
}
27+
.alert-success {
28+
color: #155724;
29+
background: #d4edda;
1530
}

0 commit comments

Comments
 (0)