-
Notifications
You must be signed in to change notification settings - Fork 0
/
end.js
23 lines (22 loc) · 791 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
const username = document.getElementById("username");
const saveScore = document.getElementById("saveScoreBtn");
const finalScore = document.getElementById("finalScore");
const mostRecentScore = localStorage.getItem('mostRecentScore');
const max_high_score = 5;
const highScores = JSON.parse(localStorage.getItem('highScores')) || [];
finalScore.innerText = mostRecentScore;
username.addEventListener("keyup", () =>{
saveScore.disabled = !username.value;
})
saveHighScore =(e) =>{
e.preventDefault();
const score ={
score: mostRecentScore,
name: username.value
};
highScores.push(score);
highScores.sort( (a,b) =>b.score - a.score);
highScores.splice(max_high_score);
localStorage.setItem('highScores', JSON.stringify(highScores));
window.location.assign('/');
};