[JavaScript] Data Structures and Modern Operators: Coding Challenge #3
![[JavaScript] Data Structures and Modern Operators: Coding Challenge #3](https://cdn.hashnode.com/res/hashnode/image/unsplash/1IW4HQuauSU/upload/v1655253943316/NR0W833f1.jpeg)
Let's continue with our football betting app! This time, we have a map called 'gameEvents' (see below) with a log of the events that happened during the game. The values are the events themselves, and the keys are the minutes in which each event happened (a football game has 90 minutes plus some extra time).
Your tasks:
Create an array 'events' of the different game events that happened (no duplicates)
After the game has finished, is was found that the yellow card from minute 64 was unfair. So remove this event from the game events log.
Compute and log the following string to the console: "An event happened, on average, every 9 minutes" (keep in mind that a game has 90 minutes)
Loop over 'gameEvents' and log each element to the console, marking whether it's in the first half or second half (after 45 min) of the game, like this: "[FIRST HALF] 17: ⚽ GOAL"
GOOD LUCK 😀
const gameEvents = new Map([
[17, '⚽ GOAL'],
[36, '🔁 Substitution'],
[47, '⚽ GOAL'],
[61, '🔁 Substitution'],
[64, '🔶 Yellow card'],
[69, '🔴 Red card'],
[70, '🔁 Substitution'],
[72, '🔁 Substitution'],
[76, '⚽ GOAL'],
[80, '⚽ GOAL'],
[92, '🔶 Yellow card'],
]);
// 1.
const events = [...new Set(gameEvents.values())];
console.log(events);
// 2.
gameEvents.delete(64);
console.log(gameEvents);
// 3.
console.log(
`An event happened, on average, every ${90 / gameEvents.size} minutes`
);
// If apply to total game-time
const time = [...gameEvents.keys()].pop();
console.log(
`An event happened, on average, every ${time / gameEvents.size} minutes`
);
// 4.
// for (const [key, value] of gameEvents) {
// if (key <= 45) console.log(`[FIRST HALF] ${key}: ${value}`);
// else console.log(`[SECOND HALF] ${key}: ${value}`);
// }
for (const [min, event] of gameEvents) {
const half = min <= 45 ? 'FIRST' : 'SECOND';
console.log(`[${half} HALF] ${min}: ${event}`);
}