0

i have a question about how a number was also associated with a string literal.

My program features a list of scorers in my game object as you can see here.

const game = {
    team1: 'Bayern Munich',
    team2: 'Borrussia Dortmund',
    players: [
      [
        'Neuer',
        'Pavard',
        'Martinez',
        'Alaba',
        'Davies',
        'Kimmich',
        'Goretzka',
        'Coman',
        'Muller',
        'Gnarby',
        'Lewandowski',
      ],
      [
        'Burki',
        'Schulz',
        'Hummels',
        'Akanji',
        'Hakimi',
        'Weigl',
        'Witsel',
        'Hazard',
        'Brandt',
        'Sancho',
        'Gotze',
      ],
    ],
    score: '4:0',
    scored: ['Lewandowski', 'Gnarby', 'Lewandowski', 'Hummels'],
    date: 'Nov 9th, 2037',
    odds: {
      team1: 1.33,
      x: 3.25,
      team2: 6.5,
    },
  };

let count0 = 0;
let count1 = 0;
let count2 = 0;

for (let i = 0; i < playerScored.length; i++) {
  if ( playerScored[i] === 'Lewandowski' ) {
    count0 += 1;
  }
  else if ( playerScored[i] === 'Gnarby') {
    count1 += 1;
  }
  else if ( playerScored[i] === 'Hummels') {
    count2 += 1;
  }
}




const scorers = {};
for (const player of game.scored) {
  scorers[player] ? scorers[player]++ : (scorers[player] = 1);
}
console.log(scorers);

My question is, in this particular line of code in the program

const scorers = {};
for (const player of game.scored) {
  scorers[player] ? scorers[player]++ : (scorers[player] = 1);
}
console.log(scorers);

How did the program show the number as well as the name of the player who scored? I kind of understand how the player name is put into the empty object. Thank you.

2
  • Do you understand what the ternary ?: operator does? Commented Jun 9, 2022 at 22:33
  • Where is playerScored defined? Commented Jun 9, 2022 at 22:35

3 Answers 3

1

The line

  scorers[player] ? scorers[player]++ : (scorers[player] = 1);

is equivalent to

if (scorers[player]) {
    scorers[player]++;
} else {
    scorers[player] = 1;
}

The if condition checks whether the name is already in the object. If it is, it uses ++ to add 1 to the count. If the name isn't already in the object, it's created with the assignment in the else block, and given an initial count of 1.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your help, but i'm confused with how it has the name as well as the number in the same line, I assume adding the ++ appends the string value with the number value as well?
it only uses ++ when the name is already in the object, so it doesn't have to append anything.
I've rewritten the answer, maybe now it's clearer.
0

The scorers object is something like this:

scorers = {
    pique: 1,
    messi: 3,
    henry: 11
};

when you do scorers[player]++ for example scorers['pique']++ It adds one to the pique values. and your object will become

scorers = {
    pique: 2,
    messi: 3,
    henry: 11
};

and when you do scorers[player] = 1 in your code, in fact you're adding a new property to the scorers object. for example after scorers['xavi'] = 1 your object becomes:

scorers = {
    pique: 2,
    messi: 3,
    henry: 11,
    xavi: 1
};

Comments

0

Your game.scored looks like this:

const game = {
  scored: ['Lewandowski', 'Gnarby', 'Lewandowski', 'Hummels']
}

When you run the following code:

const scorers = {};
for (const player of game.scored) {
  scorers[player] ? scorers[player]++ : (scorers[player] = 1);
}
console.log(scorers);

It will loop through ['Lewandowski', 'Gnarby', 'Lewandowski', 'Hummels'] and checks if there is scorers[player]. If it exists, it just increments it by 1, otherwise, sets it to 1.

Hence, after your loop is over and you console.log it, scorers will have 'Lewandowski', 'Gnarby', 'Hummels' as its keys.

I'm confused with how it has the name as well as the number in the same line, I assume adding the ++ appends the string value with the number value as well

You are basically creating an object that looks like this:

scorers = {
 Lewandowski: 2,
 Gnarby: 1,
 Hummels: 1
}

Maybe it will be easier to understand if you print the value of scorers every loop.

const game = {
  scored: ['Lewandowski', 'Gnarby', 'Lewandowski', 'Hummels']
}

const scorers = {};
for (const player of game.scored) {
  scorers[player] ? scorers[player]++ : (scorers[player] = 1);
  console.log(scorers);  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.