Home > Enterprise >  how to store and update value of an object in local storage without creating another key using Javas
how to store and update value of an object in local storage without creating another key using Javas

Time:09-27


const person={name:'',userPoints:0,computerPoints:0,Tie:0};
function evaluateResult(a,b){
    if(a == 'rock' && b == scissor){
     
        document.getElementById('demo').innerHTML="You Win";
        userpoints=userpoints 1;
        document.getElementById('userscore').innerHTML=userpoints;
        
    }
    else if(a == 'scissor' && b == rock){
        document.getElementById('demo').innerHTML="You Lose";
        computerpoints=computerpoints 1;
        document.getElementById('compscore').innerHTML=computerpoints;
    }
    else if(a == 'scissor' && b == paper){
        document.getElementById('demo').innerHTML="You Win";
        userpoints=userpoints 1;
        document.getElementById('userscore').innerHTML=userpoints;
    
    }
    else if(a == 'paper' && b == scissor){
        document.getElementById('demo').innerHTML="You Lose";
        computerpoints=computerpoints 1;
        document.getElementById('compscore').innerHTML=computerpoints;
       
     }
    else if(a == 'paper' && b == rock){
        document.getElementById('demo').innerHTML="You Win";
        userpoints=userpoints 1;
        document.getElementById('userscore').innerHTML=userpoints;
       
    }
    else if(a == 'rock' && b == paper){
        document.getElementById('demo').innerHTML="You Lose";
        computerpoints=computerpoints 1;
        document.getElementById('compscore').innerHTML=computerpoints;
       
    }
    else{
        document.getElementById('demo').innerHTML="It\'s a Tie";
        tie  ;
       
    }
 }

Suppose I have created a person object now I have to save and update the value of each key in local storage. When my function evaluateResult give an output then i want to store that result in my local storage on each iteration Is it possible to update the value of each key on every click?

CodePudding user response:

function evaluateResult(a,b){

var user=JSON.parse(localStorage.getItem('person'));

if(a == 'rock' && b == scissor){
 
    document.getElementById('demo').innerHTML="You Win";
    userpoints=userpoints 1;
    document.getElementById('userscore').innerHTML=userpoints;
   
       user.userPoints =1;
       localStorage.setItem('person',JSON.stringify(user));
}

//////////////////////////////////////////////////////////////

<<<>>> The above code represents only first case which explains the answer that how to store and update the value of an object without using another key

CodePudding user response:

From two comments to the OP's own answer ...

"Regardless of the possible solution found by the OP, the posted/provided code twice is full of type errors e.g. missing quotes for all b related comparisons like with b == scissor, b == rock and so on ... in addition the entire if...else logic could be much more simplified by a lookup based approach. "

"Another point ... since the OP generates a user, most probably an object from the stored stringified version of a user's (previous?) gaming score(s), this score needs to be addressed somehow in the code of evaluateResult ... a user object e.g. could look like this ... { scoreCounts: { won: 0, lost: 0, tie: 0 } }."

... and the suggested possible improvements from above put into an executable/running code example ...

function evaluateResult(scores, player, computer) {
  function playerWins() {
    console.log("You Win");
      scores.won;

    //document.getElementById('demo').textContent = "You Win";
    //document.getElementById('userscore').textContent =   scores.won;
  }
  function computerWins() {
    console.log("You Lose");
      scores.lost;

    //document.getElementById('demo').textContent = "You Lose";
    //document.getElementById('compscore').textContent =   scores.lost;
  }
  function itsATie() {
    console.log("It's a Tie");
      scores.tie;

    //document.getElementById('demo').innerHTML="It's a Tie";
    //  scores.tie;
  }

  (({
    // an ad hoc generated
    // object based lookup table ...

    paper_rock: playerWins,
    rock_scissor: playerWins,
    scissor_paper: playerWins,

    rock_paper: computerWins,
    scissor_rock: computerWins,
    paper_scissor: computerWins,

    // ... which immediately gets
    // accessed by a composed key ...
    //                            ... with tie fallback ...
    //                                      ... and invoked.

  }[ [player, computer].join('_') ]) ?? itsATie).call();
}

const user = {
  scoreCounts: {
    won: 0,
    lost: 0,
    tie: 0,
  },
};
console.log('user ... before evaluate ...', user);

evaluateResult(user.scoreCounts, 'rock', 'paper');
evaluateResult(user.scoreCounts, 'paper', 'rock');
evaluateResult(user.scoreCounts, 'paper', 'paper');
evaluateResult(user.scoreCounts, 'scissor', 'scissor');

console.log('user ... after evaluate ...', user);
.as-console-wrapper { min-height: 100%!important; top: 0; }

  • Related