-
Notifications
You must be signed in to change notification settings - Fork 0
/
end.js
31 lines (26 loc) · 841 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
31
const username = document.querySelector('#username')
const saveScoreBtn = document.querySelector('#saveScoreBtn')
const finalScore = document.querySelector('#finalScore')
const mostRecentScore = localStorage.getItem('mostRecentScore')
const highScores = JSON.parse(localStorage.getItem('highScores'))|| []
//MIGHT BE A PROBLEM
const MAX_HIGH_SCORES = 5
finalScore.innerText = mostRecentScore
username.addEventListener('keyup', () =>{
saveScoreBtn.disabled = !username.value
})
saveHighScore = e=>{
e.preventDefault();
const score = {
score: mostRecentScore,
name: username.value,
};
highScores.push(score);
highScores.sort((a, b) => {
return b.score - a.score;
});
//MIGHT BE A PROBLEM
highScores.splice(5);
localStorage.setItem('highScores', JSON.stringify(highScores))
window.location.assign('/')
}