From 1fb4e3f8e375ea3937a448119b701b62f0feb56d Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Wed, 19 Apr 2023 19:56:51 +0200 Subject: [PATCH] add statistic class --- js/statistics.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 js/statistics.js diff --git a/js/statistics.js b/js/statistics.js new file mode 100644 index 0000000..d7aa863 --- /dev/null +++ b/js/statistics.js @@ -0,0 +1,23 @@ +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); + } + } +} \ No newline at end of file