-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighscores.js
40 lines (33 loc) · 1.06 KB
/
highscores.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
// Hergemony Quiz - Scoreboard Page
// Declare the variables
var highScore = document.querySelector("#highScore");
var clear = document.querySelector("#clear");
var goBack = document.querySelector("#goBack");
// Add Event Listeners to clear the scores
clear.addEventListener("click", function () {
localStorage.clear();
location.reload();
});
// Retrieve what was stored on local storage
var allScores = localStorage.getItem("allScores");
allScores = JSON.parse(allScores);
if (allScores !== null) {
// sort function added to rank scores from highest to lowest
allScores.sort(function (a, b) {
debugger;
if (b.score > a.score) {
return 1;
} else {
return -1;
}
});
for (var i = 0; i < allScores.length; i++) {
var createLi = document.createElement("li");
createLi.textContent = allScores[i].initials + " " + allScores[i].score;
highScore.appendChild(createLi);
}
}
// Add event listener to enable user to move back to index/first page
goBack.addEventListener("click", function () {
window.location.replace("./index.html");
});