CpuHigherLower/js/statistics.js

23 lines
573 B
JavaScript
Raw Normal View History

2023-04-19 19:56:51 +02:00
class Stats {
#score;
#highScore;
#highScoreStorageKey;
constructor(highScoreStorageKey) {
// used as key for localStorage
this.#highScoreStorageKey = highScoreStorageKey;
this.#score = 0;
this.#highScore = localStorage.getItem(this.#highScoreStorageKey) ?? 0;
}
incrementScore(value = 1) {
this.#score += value;
if (this.#highScore < this.#score) {
this.#highScore = this.#score;
localStorage.setItem(this.#highScoreStorageKey, this.#highScore);
}
}
}