-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathend.js
26 lines (22 loc) · 812 Bytes
/
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
const username = document.getElementById("username");
const saveScoreBtn = document.getElementById("saveScoreButton");
const mostRecentScore = localStorage.getItem("mostRecentScore");
const highScore = JSON.parse(localStorage.getItem("highScore")) || [];
const finalScore = document.getElementById("finalScore");
finalScore.innerText = mostRecentScore;
username.addEventListener("keyup", () => {
saveScoreBtn.disabled = !username.value;
});
saveHighScore = (e) => {
e.preventDefault();
// console.log("button is clicked");
const hScore = {
score: mostRecentScore,
username: username.value,
};
highScore.push(hScore);
highScore.sort((a, b) => b.score - a.score);
highScore.splice(5);
localStorage.setItem("highScore", JSON.stringify(highScore));
window.location = "index.html";
};