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));
}
}
scorecardInputwithout it being defined. Do you have a full example available?