Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.

edit: i did another check, the array is being ovewritten to 0 when the functions in the init occour, even with hardcoded values, no idea why

JavaScript
`var gBestScores =[]`

in the init i have:
JavaScript
var retrievedData = localStorage.getItem("scores");
        gBestScores = JSON.parse(retrievedData);
        bestScore.innerHTML = gBestScores[gBestScores.length - 1]

the rest of it:
JavaScript
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
    }


What I have tried:

tried debuggeing, it does come back as null, no idea how to fix tho since it worked before
Posted
Updated 25-Jan-20 10:02am
v4
Comments
[no name] 26-Jan-20 11:16am    
Try a cookie.
ZurdoDev 27-Jan-20 8:17am    
Debug the code and check localStorage right after you write to it.
gggustafson 7-Feb-20 13:06pm    
May I suggest that you rework your design. In your checkGameStatus you perform too many actions (the rule is write a statement that describes what the method does; if it includes 'and', 'when', 'after', etc., your method needs to be broken up). I'd move all localStorage actions to their own methods (e.g., get_local_storage, save_local_storage). That will make debugging much easier. That means that checkGameStatus should invoke get_local_storage, perform some comparisons, and, if needed, invoke save_local_storage. Also, use parameters, they localize values to within the invoked method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900