Last active
May 1, 2020 13:49
-
-
Save ross-u/c7ae162b3c0b876ef89d56b131055789 to your computer and use it in GitHub Desktop.
localStorage - example 01/20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function updateScore(name, score) { | |
// GET PREVIOUS SCORE | |
let previousScore = JSON.parse(localStorage.getItem("score")); | |
if (!previousScore) { | |
previousScore = []; | |
} | |
// UPDATE THE SCORE | |
const newScore = { name, score }; | |
previousScore.push(newScore); | |
// SET THE SCORE BACK TO LOCAL STORAGE | |
const updatedScoreStr = JSON.stringify(previousScore); | |
localStorage.setItem("score", updatedScoreStr); | |
} | |
updateScore("bob", 123); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment