Implementing High Scores
Let's now implement the last missing feature of our application: high scores. The high scores work the following way:
Let's start by declaring the variable highScore
and initializing it at zero
let highScore = 0;
Now, in the check button event handler, when checking if the user guess is equal to the secret number, add the below code because it's only when the user's guess is equal to the secret number that we set a new high score.
if (score > highScore) {
highScore = score;
document.querySelector('.highscore').textContent = highScore;
}
Now, if you reload and play it for the first time, you will notice that your high score is zero. Then if you win, your high score will be equal to your current score. Then, if your score is greater than your current high score, the latter will be updated the second time you play.