-
Notifications
You must be signed in to change notification settings - Fork 0
/
end.js
30 lines (27 loc) · 968 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
27
28
29
30
const game = document.getElementById("username");
const saveScoreBtn = document.getElementById("saveScoreBtn");
const finalScore = document.getElementById("finalScore");
const mostRecentScore = localStorage.getItem("mostRecentScore");
// localStorage.setItem("highScores", JSON.stringify([]));
const highScores = JSON.parse(localStorage.getItem("highScores")) || [];
console.log(highScores);
finalScore.innerText = mostRecentScore;
username.addEventListener("keyup", () => {
saveScoreBtn.disabled = !username.value;
});
saveHightScore = e => {
e.preventDefault();
console.log("hello");
const score = {
score: Math.floor(Math.random() * 100),
name: username.value
};
console.log(score.score);
highScores.push(score);
highScores.sort((a, b) => b.score - a.score);
highScores.splice(5);
localStorage.setItem("highScores", JSON.stringify(highScores));
window.location.assign("/");
console.log(highScores);
console.log(score.score);
};