-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscores.js
30 lines (23 loc) · 886 Bytes
/
scores.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
function printHighscores() {
// either get scores from localstorage or set to empty array
var highscores = JSON.parse(window.localStorage.getItem("highscores")) || [];
// sort highscores by score property in descending order
highscores.sort(function(a, b) {
return b.score - a.score;
});
highscores.forEach(function(score) {
// create li tag for each high score
var liTag = document.createElement("li");
liTag.textContent = score.initials + " - " + score.score;
// display on page
var olEl = document.getElementById("highscores");
olEl.appendChild(liTag);
});
}
function clearHighscores() {
window.localStorage.removeItem("highscores");
window.location.reload();
}
document.getElementById("clear").onclick = clearHighscores;
// run function when page loads
printHighscores();