-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathend.js
32 lines (24 loc) · 1.02 KB
/
end.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
const username = document.getElementById("username");
const saveScoreBtn = document.getElementById("saveScoreBtn");
const finalScore = document.getElementById("finalScore");
const mostRecentScore = localStorage.getItem("mostRecentScore");
const highScores = JSON.parse(localStorage.getItem("highScores")) || [];
console.log(mostRecentScore)
const MAX_HIGH_SCORES = 5;
finalScore.innerText = mostRecentScore;
username.addEventListener("keyup", () => {
saveScoreBtn.disabled = !username.value;
});
saveHighScore = e => {
console.log("clicked the save button!");
e.preventDefault();
const score = {
score: mostRecentScore,
name: username.value
};
highScores.push(score); // adds the item to the end of the array
highScores.sort((a, b) => b.score - a.score); //orders an array of objects, depending on one of the values
highScores.splice(5); // removes from array what is after position 4
localStorage.setItem("highScores", JSON.stringify(highScores));
window.location.assign("/");
};