From bc69cc9e0acff576786ec42762be313128de9dad Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Fri, 14 Apr 2023 00:53:31 +0200 Subject: [PATCH] add score and highscore --- gst.html | 5 ++++- gst.js | 24 +++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/gst.html b/gst.html index a2a1fbf..d8c42f6 100644 --- a/gst.html +++ b/gst.html @@ -46,10 +46,13 @@
Guess the socket-type of the CPU +
+ Score: 0 + Highscore: 0
-
+

diff --git a/gst.js b/gst.js index 7029e2e..61ebc0c 100644 --- a/gst.js +++ b/gst.js @@ -10,6 +10,10 @@ export async function main() { .then((response) => response.json()) .then((json) => cpuList = json)); + highScore = localStorage.getItem("highScore_socket") ?? 0 + score = 0; + updateScores(); + nextRound(); } @@ -83,7 +87,20 @@ export async function btnClick(typ) { btnServer.style.backgroundColor = "lightgreen"; } - await delay(2000); + // Score + if (btns[typ].style.backgroundColor == "lightgreen") { + score++; + } else { + score = 0; + } + // Highscore + if (score > highScore) { + highScore = score; + localStorage.setItem("highScore_socket", highScore); + } + updateScores(); + + await delay(1000); btns.forEach( (el) => { el.style.backgroundColor = "#3CC3FA"; @@ -93,6 +110,11 @@ export async function btnClick(typ) { nextRound(); } +function updateScores() { + document.getElementById("score").innerText = score; + document.getElementById("highScore").innerText = highScore; +} + function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max);