Rusttune
Rusttune

Reputation: 39

How can I increment a variable within an object that is within an array based on certain conditions?

I'm brand new to coding and javascript (4 weeks) so forgive me if this is pretty basic.

I'm working on creating a Disc Golf score tracker web application that works like this: you input the course, player names, and scores and on submit, it creates a scorecard and outputs it to the page. From there I want it to determine who the winner for each round is and then calculate a win/loss ratio for all available scorecards, which will then output to a leaderboard.

I'm stuck on changing the value of the win/loss variables in my array of objects.

My idea for changing this was to have it happen before it is put into local storage, when each scorecard is submitted. I've tried to used conditional statements like below but they don't seem to work. Should this be done in a separate function? I currently have the array of objects being created, followed by these variables being created, and then the conditional statement below. Is this in the wrong order? I've been stuck on this for a while and just can figure out what I'm doing wrong.

let scoreOneInput = scorecardInput[1].score;
let winOne = scorecardInput[1].win;
let lossOne = scorecardInput[1].loss;

let scoreTwoInput = scorecardInput[2].score;
let winTwo = scorecardInput[2].win;
let lossTwo = scorecardInput[2].loss;

if (scoreOneInput <= scoreTwoInput) {
  winOne++;
};

function submitScorecard() {

  let scorecardInput = [
    {
      courseName: course
    },
    {
      name: playerOne,
      score: scoreOne,
      win: 0,
      loss: 0
    },
    {
      name: playerTwo,
      score: scoreTwo,
      win: 0,
      loss: 0
    },
    {
      name: playerThree,
      score: scoreThree,
      win: 0,
      loss: 0
    },
    {
      name: playerFour,
      score: scoreFour,
      win: 0,
      loss: 0
    },
    {
      name: playerFive,
      score: scoreFive,
      win: 0,
      loss: 0
    }
  ];

  if (localStorage.getItem('scorecard') === null) {
    let scorecard = [];
    scorecard.push(scorecardInput);
    localStorage.setItem('scorecard', JSON.stringify(scorecard));
  } else {
    let scorecard = JSON.parse(localStorage.getItem('scorecard'));
    scorecard.push(scorecardInput);
    localStorage.setItem('scorecard', JSON.stringify(scorecard));
  }
}

Upvotes: 1

Views: 106

Answers (2)

nushen
nushen

Reputation: 61

I think you have to calculate the maximum score in the round. You shouldn't just compare two scores. So first, you have to reorganize your scorecardInput:

let scorecardInput = [
courseName: course
players: [
   {
     name: playerOne,
     score: scoreOne,
     win: 0,
     loss: 0
   },
   {
     name: playerTwo,
     score: scoreTwo,
     win: 0,
     loss: 0
   },
   ...
]
];

Then you calculate the maximum score and increment the number of wins for that player:

   scorecardInput.players = scorecardInput.players.map(function(player) {
      if(player.score == Math.max.apply(Math,scorecardInput.players.map(function(p) { 
                    return p.score;  
                })))
             player.win++;
             return player;
      })
    }
  );

Upvotes: 0

Alex Wayne
Alex Wayne

Reputation: 187004

Numbers are passed by reference in javascript, meaning each variable its own unique instance. Changing one, will not change another.

For example:

let a = 1
let b = a
a++
console.log(a) //=> 2
console.log(b) //=> 1

You need to assign your new number back into your data structure:

if (scoreOneInput <= scoreTwoInput) {
  scorecardInput[2].win = scorecardInput[2].win + 1;
};

Or you could increment the number directly on your scorecard object:

if (scoreOneInput <= scoreTwoInput) {
  scorecardInput[2].win++;
};

Upvotes: 2

Related Questions