-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathend.js
More file actions
34 lines (24 loc) · 898 Bytes
/
end.js
File metadata and controls
34 lines (24 loc) · 898 Bytes
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
33
34
const username = document.getElementById('username');
const saveScore = document.getElementById('save-score-btn');
const finalScore = document.getElementById('final-score');
const mostRecentScore = localStorage.getItem("mostRecentScore");
const highScores = JSON.parse(localStorage.getItem("highScore")) || [];
const max_high_scores = 5;
console.log(highScores);
finalScore.innerText = mostRecentScore;
username.addEventListener("keyup", () => {
saveScore.disabled = !username.value;
});
saveHighScore = (e) => {
alert("Your score is saved!");
e.preventDefault();
const score = {
score : mostRecentScore,
name : username.value
};
highScores.push(score);
highScores.sort( (a,b) => b.score-a.score )
highScores.splice(max_high_scores);
localStorage.setItem('highScore', JSON.stringify(highScores));
window.location.assign('index.html');
};