Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exercise 8k i checked my code and its the same but still giving error #132

Open
unevtable opened this issue Jun 26, 2024 · 7 comments
Open

Comments

@unevtable
Copy link

<!DOCTYPE html>
<html>

<head>
  <title>Coin Flip</title>
</head>

<body>
  <button onclick="
    playGame('HEADS');
  ">Heads</button>
  <button onclick="
    playGame('TAILS');
  ">Tails</button>

  <script>
    const score = JSON.parse(localStorage.getItem('score')) || { wins: 0, losses: 0 };

    function playGame(guess) {
      const randomNumber = Math.random();
      const result = randomNumber < 0.5 ? 'HEADS' : 'TAILS';

      console.log(`ITS ${result}!`);

      console.log(guess === result ? 'You Win!' : 'You Lose!');

      guess === result ? score.wins++ : score.losses++;
      console.log(score);

      localStorage.setItem('score', JSON.stringify(score));
    }
  </script>
</body>

</html>
@unevtable
Copy link
Author

image

@kavin00111
Copy link

Its working correctly I don't think there is any error

@MoudiZd
Copy link

MoudiZd commented Jun 30, 2024

I think its about javascript version inside browser

What is the browser you are using and which version, btw try to update your browser than run your code i think it must works ...

@unevtable
Copy link
Author

i am using edge and it is the latest version

@MoudiZd
Copy link

MoudiZd commented Jul 1, 2024

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

into

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

@unevtable
Copy link
Author

can you please explain this

@MoudiZd
Copy link

MoudiZd commented Jul 2, 2024

can you please explain this

First let tell what is the problem:

Using JSON.parse(null); will result null, while using JSON.parse(undefined); throws an exception and stop the script

For some reason using localStorage.getItem('score'); on microsoft edge returns undefined (browser specific behavior...)

So using :
JSON.parse(localStorage.getItem('score')) || {wins: 0,losses: 0};

Will be equal to:
JSON.parse(undefined) || {wins: 0,losses: 0}

And because:
JSON.parse(undefined) throws an error this leads to not be fallen back to the passed default object, but instead stop the script

For that we will try to find a way that letJSON.parse not stop the script but results null instead when the passed argument is undefined, and this can be done by adding || null to the argument passed to JSON.parse call

To become:
JSON.parse(localStorage.getItem('score')||null) || { wins: 0, losses: 0 };

In this way the script will not be stopped due passing undefined to JSON.parse call, because it will see that we are passing null instead of undefined (because undefined || null = null) which is acceptable by parsemethod therefore will result overall to return the default object passed after theJSON.parse` call due that the first part of statement is null

Hope that this was clear enough to address the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants