-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathend.js
47 lines (37 loc) · 1.48 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const username = document.getElementById('username');
const saveScoreBtn = document.getElementById('saveScoreBtn');
const finalScore = document.getElementById('finalScore');
const mostRecentScore = localStorage.getItem('mostRecentScore');
const highScoresGenKnowledge = JSON.parse(localStorage.getItem('highScoresGenKnowledge')) || [];
const highScoresCompScience = JSON.parse(localStorage.getItem('highScoresCompScience')) || [];
const MAX_HIGH_SCORES = 5;
finalScore.innerText = mostRecentScore;
getCategoryname =() =>{
const url = window.location.href;
return url.slice(url.indexOf("=")+1);
}
let catName = getCategoryname();
username.addEventListener('keyup', () => {
saveScoreBtn.disabled = !username.value;
});
saveHighScore = (e) => {
e.preventDefault();
const score = {
score: mostRecentScore,
name: username.value,
};
//If condition here
if(catName === "compScience"){
highScoresCompScience.push(score);
highScoresCompScience.sort((a, b) => b.score - a.score);
highScoresCompScience.splice(5);
}
else if(catName === "genKnowledge"){
highScoresGenKnowledge.push(score);
highScoresGenKnowledge.sort((a, b) => b.score - a.score);
highScoresGenKnowledge.splice(5);
}
localStorage.setItem('highScoresCompScience', JSON.stringify(highScoresCompScience));
localStorage.setItem('highScoresGenKnowledge', JSON.stringify(highScoresGenKnowledge));
window.location.assign('/');
};