Skip to content

Commit c553fe0

Browse files
feat: display how many answers the users got correct on the end screen.
* added number of score in last page * n * Update pages/end.html * small updates * small updates * updates --------- Co-authored-by: Kendall <kendalldoescoding@gmail.com>
1 parent ca0a470 commit c553fe0

5 files changed

Lines changed: 72 additions & 52 deletions

File tree

css/index.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
h1 {
1717
font-size: 5.4rem;
1818
color: #fff;
19-
margin-bottom: 5rem;
19+
margin-bottom: 3rem;
2020
text-align: center;
2121
}
2222

js/QuizClass.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,30 @@
44
export class Quiz {
55
constructor(dataQA = []) {
66
this.dataQA = dataQA;
7-
// initial game values
87
this.QUESTION_VALUE = 100;
98
this.QUESTIONS_AMOUNT = dataQA.length;
109
this.barPercetage = 0;
1110
this.score = 0;
1211
this.answer = "";
1312
this.canClick = true;
14-
// runs here becasue we want to load the first round of questions
13+
this.noOfCorrect = 0;
14+
this.TOTAL_CORRECT = 0;
15+
1516
this._renderNewQuestion();
1617
}
1718

18-
// if there's questions left, return the last one
1919
_newQuestion() {
2020
if (this.getQuestionsLen === 0) this._endGame();
2121
return this.dataQA.pop();
2222
}
2323

24-
// takes care of managing the percentage green bar on top
2524
_renderPercentage() {
2625
const leftQA = this.dataQA.length;
2726
const percentage = (1 - leftQA / this.QUESTIONS_AMOUNT) * 100;
2827

2928
document.getElementById("progressBarFull").style.width = `${percentage}%`;
3029
}
3130

32-
// need to calculate it based on the total amount, and remaining amount
3331
_renderQuestionNumber() {
3432
const leftQA = this.dataQA.length;
3533
const currentQA = this.QUESTIONS_AMOUNT - leftQA;
@@ -38,7 +36,6 @@ export class Quiz {
3836
paragraph.textContent = `Question ${currentQA} of ${this.QUESTIONS_AMOUNT}`;
3937
}
4038

41-
// get's a new question from newQuestion & renders it to the page
4239
_renderNewQuestion() {
4340
const currentQA = this._newQuestion();
4441
if (!currentQA) {
@@ -54,43 +51,47 @@ export class Quiz {
5451

5552
this.answer = currentQA["answer"];
5653

57-
// render question && all options
5854
document.getElementById("question").textContent = currentQA["question"];
5955

6056
pArray.forEach((p, i) => {
6157
p.textContent = currentQA[`choice${i + 1}`];
6258
});
6359
}
6460

65-
// sends the user to the highscore.html and asks if they want to save their score
6661
_endGame() {
67-
// console.log("GAME ENDED");
6862
window.localStorage.setItem("mostRecentScore", this.score);
63+
window.localStorage.setItem("noofanswerscorrect", this.noOfCorrect);
64+
window.localStorage.setItem("noofquestions", this.QUESTIONS_AMOUNT);
65+
6966
window.location.assign("/pages/end.html");
7067
}
68+
69+
70+
71+
_updateCorrectCount() {
72+
this.TOTAL_CORRECT++;
73+
console.log("Total Correct:", this.TOTAL_CORRECT);
74+
}
7175

7276
checkAnswer(selected = 0, correct = 0) {
7377
const p = document.querySelector(`[data-number="${selected}"]`);
7478

7579
if (!this.canClick) {
76-
// console.log("WAIT THERE BOI");
7780
return;
7881
}
7982

8083
if (selected === correct) {
8184
p.parentElement.classList.add("correct");
8285
this.score += this.QUESTION_VALUE;
83-
// console.log("NOICE: ", this.score);
86+
this.noOfCorrect++;
87+
this._updateCorrectCount();
8488
} else {
8589
p.parentElement.classList.add("incorrect");
8690
this.score -= this.QUESTION_VALUE;
87-
// console.log("BAKA GA!: ", this.score);
8891
}
8992

90-
// render updated score
9193
document.getElementById("score").textContent = this.score;
9294

93-
// delay stuff
9495
this.canClick = false;
9596

9697
setTimeout(() => {

js/end.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
const username = document.querySelector("#username");
22
const saveScoreBtn = document.querySelector("#saveScoreBtn");
33
const finalScore = document.querySelector("#finalScore");
4+
const noScore = document.querySelector("#noScore");
45
const mostRecentScore = localStorage.getItem("mostRecentScore");
6+
const noOfCorrect = localStorage.getItem("noofanswerscorrect");
7+
const totalQuestions = localStorage.getItem("noofquestions");
58

69
const highScores = JSON.parse(localStorage.getItem("highScores")) || [];
710

811
const MAX_HIGH_SCORES = 5;
912

1013
finalScore.innerText = mostRecentScore;
14+
noScore.innerText = `${noOfCorrect}/${totalQuestions}`;
1115

1216
username.addEventListener("keyup", () => {
1317
saveScoreBtn.disabled = !username.value;
@@ -26,8 +30,12 @@ saveHighScore = (e) => {
2630
highScores.sort((a, b) => {
2731
return b.score - a.score;
2832
});
29-
highScores.splice(5);
33+
highScores.splice(MAX_HIGH_SCORES);
3034

3135
localStorage.setItem("highScores", JSON.stringify(highScores));
3236
window.location.assign("/");
3337
};
38+
39+
function goBack(n) {
40+
history.go(n);
41+
}

pages/end.html

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,52 @@
11
<!DOCTYPE html>
22
<html lang="en">
33

4-
<head>
5-
<meta charset="UTF-8" />
6-
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
7-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
8-
9-
<title>End Page</title>
10-
11-
<script src="../js/end.js" defer></script>
12-
<link rel="stylesheet" href="../css/index.css" />
13-
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.4/css/all.css"
14-
integrity="sha384-DyZ88mC6Up2uqS4h/KRgHuoeGwBcD4Ng9SiP4dIRy0EXTlnuz47vAwmeGwVChigm" crossorigin="anonymous" />
15-
</head>
16-
17-
<body>
18-
<div class="container">
19-
<div id="end" class="flex-center flex-column">
20-
<h1 id="finalScore">0</h1>
21-
<form class="end-form-container">
22-
<h2 id="end-text">Enter your name below to save your score!</h2>
23-
<input type="text" name="name" id="username" placeholder="Enter your name" />
24-
<button class="btn" id="saveScoreBtn" type="submit" onclick="saveHighScore(event)" disabled>
25-
Save
26-
</button>
27-
</form>
28-
29-
<p class="btn" onclick="goBack(-1)">Play Again</p>
30-
31-
<p onclick="goBack(-2)" class="btn" target="_blank">Go back to Quiz Home Page Menu<i class="fas fa-home"></i>
32-
</p>
33-
<a href="/rating.html" class="btn" target="_blank">Please rate our quiz!<i class="far fa-star"></i></a>
4+
<head>
5+
<meta charset="UTF-8" />
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
8+
9+
<title>End Page</title>
10+
11+
<script src="../js/end.js" defer></script>
12+
<link rel="stylesheet" href="../css/index.css" />
13+
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.4/css/all.css"
14+
integrity="sha384-DyZ88mC6Up2uqS4h/KRgHuoeGwBcD4Ng9SiP4dIRy0EXTlnuz47vAwmeGwVChigm" crossorigin="anonymous" />
15+
</head>
16+
17+
<body>
18+
<div class="container">
19+
<div id="end" class="flex-center flex-column">
20+
<h1 id="finalScore">0</h1>
21+
<div style="color: white; font-size: 30px; margin-bottom: 1rem;">
22+
<p>You got <span id="noScore" style="font-size: 30px;">0</span> correct. </p>
23+
3424
</div>
25+
<form class="end-form-container">
26+
<h2 id="end-text">Enter your name below to save your score!</h2>
27+
<input type="text" name="name" id="username" placeholder="Enter your name" />
28+
29+
<button class="btn" id="saveScoreBtn" type="submit" onclick="saveHighScore(event)" disabled>
30+
Save
31+
</button>
32+
</form>
33+
34+
<p class="btn" onclick="goBack(-1)">Play Again</p>
35+
36+
<p onclick="goBack(-2)" class="btn" target="_blank">Go back to Quiz Home Page Menu<i class="fas fa-home"></i>
37+
</p>
38+
<a href="/rating.html" class="btn" target="_blank">Please rate our quiz!<i class="far fa-star"></i></a>
3539
</div>
36-
</body>
40+
</div>
41+
</body>
42+
43+
<script>
44+
const totalScore = localStorage.getItem("noofquestions");
45+
document.getElementById("totalScore").textContent = totalScore;
3746

38-
<script>
39-
function goBack(n) {
40-
history.go(n);
41-
}
42-
</script>
47+
function goBack(n) {
48+
history.go(n);
49+
}
50+
</script>
4351

4452
</html>

pages/game.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
import { gameQA } from "../js/data/index.js";
1515
import { Quiz } from "../js/QuizClass.js";
1616

17+
18+
1719
const QuizInstance = new Quiz(gameQA);
1820

1921
document.addEventListener("click", (e) => {
2022
if (e.target.dataset.number) {
2123
QuizInstance.checkAnswer(+e.target.dataset.number, QuizInstance.answer);
24+
2225
}
2326
});
2427
</script>

0 commit comments

Comments
 (0)