I was trying to set local storage of best game times, so I created an empty array gBestScores
and compared the value of the last best score to the current one,
and depending on the result I either push it or not to the array.
The local storage supposed to save it. The saving doesn't happen and the array stays empty. The currScore
is showing the correct clock content.
lastBestScore
is undefined. Iv'e tried hard coding 2 start times to the -1 won't come back as unidentified, that didn't work, and it work beforehand without doing that
the comparison and the storage worked, i have no idea what i changed, ctrl-Z failed to save me, hope you can.
second update: array gets overwritten to nothing even with hardcoded values, no idea what causes this
var gBestScores =[]
in the init i have:
var retrievedData = localStorage.getItem("scores");
gBestScores = JSON.parse(retrievedData);
bestScore.innerHTML = gBestScores[gBestScores.length - 1]
the rest of it:
function checkGameStatus() {
// debugger
var currScore = result.innerHTML
var lastBestScore = gBestScores[gBestScores.length-1]
if ((gLevel.SIZE ** 2 - gLevel.MINES) === gGame.shownCount && gLevel.MINES === gGame.markedCount) {
gElBtn.innerHTML = WIN
gGame.isOn = true
stopwatch.stop()
if (currScore > lastBestScore) {
gBestScores.push(currScore)
}
}
else if (gGame.isOn === true) {
gElBtn.innerHTML = SAD
stopwatch.stop()
if (currScore > lastBestScore) {
gBestScores.push(currScore)
}
}
localStorage.setItem("scores", JSON.stringify(gBestScores));
bestScore.innerHTML = lastBestScore
}