Data Structures and Modern Operators

section Coding Challenge #2. jonas s Udemy Js course

by trentHarlem

HTML

<p>
// Coding Challenge #2<br><br>

Let's continue with our football betting app!<br>

1. Loop over the game.scored array and print each player name to the console, along with the goal number (Example: "Goal 1: Lewandowski")<br><br>
2. Use a loop to calculate the average odd and log it to the console (We already studied how to calculate averages, you can go check if you don't remember)<br><br>
3. Print the 3 odds to the console, but in a nice formatted way, exactly like this:<br>
      Odd of victory Bayern Munich: 1.33<br>
      Odd of draw: 3.25<br>
      Odd of victory Borrussia Dortmund: 6.5<br><br>
Get the team names directly from the game object, don't hardcode them (except for "draw").<br><br>
HINT: Note how the odds and the game objects have the same property names 😉<br><br>
BONUS: Create an object called 'scorers' which contains the names of the players who scored as properties, and the number of goals as the value. In this game, it will look like this:<br>
      {
        Gnarby: 1,
        Hummels: 1,
        Lewandowski: 2
      }<br>

GOOD LUCK 😀

</p>

CSS

html {
  font: 1.1em system-ui;
}

JavaScript

'use strict'
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,
  },
};
////////////////////// challenge #2
//1
const scored = game.scored
for (let player of scored.entries()) {
  console.log(`Goal ${player[0]+1}: ${player[1]}`)
}
//2
/* function calcOdds() { */
const oddsValues = Object.values(game.odds)
console.log(oddsValues)
let sum = 0;
for (let odd of oddsValues) {
  sum += odd
}
console.log(sum /= oddsValues.length)
//return sum /= oddsValues.length
/* } calcOdds() */

//3
for (const [key, value] of Object.entries(game.odds)) {
  //console.log(key, value)
  key === 'x' ?
    console.log(`Odd of draw: ${value}`) :
    console.log(`Odd of victory ${game[key]}: ${value}`);
}

//   BONUS.
// solution 1
const scorers = {};

for (const player of game.scored) {
//scorers[player] = 1;
//if scorers.player exists will add 1 to player value(goals)
scorers[player] ? scorers[player]++ : (scorers[player] = 1);
}

// solution 2
/* const scorers = game.scored.reduce((o, key) => {
  o[key] ? o[key]++ : o[key] = 1;
  return o;
}, {}); */

console.log(
  scorers
);

/*
// 1.
for (const [i, player] of game.scored.entries())
  console.log(`Goal ${i + 1}: ${player}`);

// 2.
const odds = Object.values(game.odds);
let average = 0;
for (const odd of odds) average += odd;
average /= odds.length;
console.log(average);

// 3.
for (const [team, odd] of Object.entries(game.odds)) {
  const teamStr = team === 'x' ?...