I'm currently developing a cookie clicker website and am encountering an issue with saving the user's score to localstorage when they click the "save" button. Here is what my code looks like:
let score = 0;
function addPoint() {
score += 1;
}
if (localStorage.getItem("score") !== null) {
score = parseInt(localStorage.getItem("score"));
}
function saveGame() {
localStorage.setItem("score", score);
alert("Score has been saved successfully!");
}
The current functionality somewhat works, but there's a problem when the score variable changes. Since the score is saved as a string, if the user already had points and then gains more, it concatenates rather than adding properly. For example, starting with five points and saving the game, earning another point would result in 51 points instead of 6. I need assistance resolving this issue quickly. I'm unsure whether going with cookies is a better approach. Appreciate any insights!