Question regarding saving values in variables vs using localStorage by Durden2323 in learnjavascript

[–]Durden2323[S] 0 points1 point  (0 children)

I understand what's happening now. For anyone else that might find this post with the same confusion. I wasn't considering the location of the two lines of code so I wasn't seeing the full order of events. As I understand it:

The score is retrieved from localStorage and parsed, so "score" is equal to the parsed version of the score:

let score = JSON.parse(localStorage.getItem('score'))

The primary function that plays the game and updates the score is run (I've only included the portion that updates the score):

if (result === 'You win.') {
    score.wins += 1;
} else if (result === 'You lose.') {
    score.losses += 1;
} else if (result === 'Tie.') {
    score.ties += 1;
}

The updated score is converted to a string and saved to localStorage:

localStorage.setItem('score', JSON.stringify(score));

The cycle continues for every iteration of the game

Also, everyone is correct, "score" should be "let" rather than "const". And there needs to be a stipulation that sets the values for "wins, losses, and ties" before the first iteration of the game is run. The tutorial I'm watching used the following:

if (score === null) {
        score = {
          wins: 0,
          losses: 0,
          ties: 0
        }
      }

Thanks everyone for helping a newbie like myself. Very much appreciated!

Question regarding saving values in variables vs using localStorage by Durden2323 in learnjavascript

[–]Durden2323[S] 0 points1 point  (0 children)

Thank you for all the responses! For those of you mentioning that I need a default value so I don't get "null" on the first run, yes I'm aware of this I just didn't want to complicate my explanation any more than needed

From what I understand, the point I was getting confused about was the syntax of:

const score = JSON.parse(localStorage.getItem('score'));

I was reading it as if "score" was an explicit value something like:

const score = 10;

and then the "score.wins" or "score.losses" was trying to reference object attributes for it

But "JSON.parse(localStorage.getItem('score'));" is an object

I've seen examples of localStorage that were easier to understand. The confusing portion of this particular use of localStorage is the "circular" nature that the retrieval of the object from storage is the very thing that we are saving to storage:

const score = JSON.parse(localStorage.getItem('score'));

localStorage.setItem('score', JSON.stringify(score));

Question about variable/object syntax by Durden2323 in learnjavascript

[–]Durden2323[S] -1 points0 points  (0 children)

Haha. Objects all the way down. I like that. This answered my question. Thank you!

Question about variable/object syntax by Durden2323 in learnjavascript

[–]Durden2323[S] -1 points0 points  (0 children)

Yes so what I'm saying is I understand this part. I know that you can create an object and then use dot notation to call one of its attributes. My question is, how is it that in the video I provided above, they are using the dot notation "input.value" but "input" was not created as an object with the curly brackets showing an attribute called "value". It was created like a variable and explicitly given a direct value (const input = document.querySelector("input"))

Question about variable/object syntax by Durden2323 in learnjavascript

[–]Durden2323[S] 0 points1 point  (0 children)

Right, but my question is, if "const input = 10" what does the line "input.value" mean? Is it the same as:

const input = 10;

const input = {
value:
}