SuperSimpleDev / javascript-course

737 stars 588 forks source link

SyntaxError #86

Open esto30 opened 6 months ago

esto30 commented 6 months ago

I couldn't understand this error //// Uncaught SyntaxError: "undefined" is not valid JSON at JSON.parse () at 10-rock-paper-scissor copy.html:43:27 ////

let scores = JSON.parse(localStorage.getItem('scores')) || { wins:0, losses:0, ties:0 }; . . . . localStorage.setItem('scores', JSON.stringify(scores));

unevtable commented 3 months ago

same error bro

MoudiZd commented 3 months ago

Which browser and which version you are using???

It seems a browser behavior error which return undefined instead of null when the searched storage item does not exist yet

unevtable commented 3 months ago

microsoft edge and latest version

MoudiZd commented 3 months ago

Try to change this line : let scores = JSON.parse(localStorage.getItem('scores')) || { wins: 0, losses: 0 };

into

let scores = JSON.parse(localStorage.getItem('scores')||null) || { wins: 0, losses: 0 };