Coding Challenge #3

from Data Structures section Udemy Jonas S JavaScript

by trentHarlem

HTML

<p>
// Coding Challenge #3<br>
<br>
Let's continue with our football betting app! This time, we have a map 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).
<br>
1. Create an array 'events' of the different game events that happened (no duplicates)<br>
2. 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.<br>
3. Print the following string to the console: "An event happened, on average, every 9 minutes" (keep in mind that a game has 90 minutes)<br>
4. Loop over the events and log them to the console, marking whether it's in the first half or second half (after 45 min) of the game, like this:<br>
      [FIRST HALF] 17: ⚽️ GOAL<br>

GOOD LUCK πŸ˜€
</p>

CSS

html {
  font: 1.1em system-ui;
}

JavaScript

//'use strict';

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'],
]);

const timesArray = [];
// 1. Create an array (of unique)'events'

//  used a Set to filter duplicates 
// destructured  the data to isolate the events and ignore the time.
const uniqueEvents = new Set()
for (const [t, e] of gameEvents) {
  timesArray.unshift(t)
  uniqueEvents.add(e);
}
/* const uniqueEvents = [...new Set(gameEvents.values())]
console.log('ue',uniqueEvents) */
// 1.
const events = Array.from(uniqueEvents);
console.log(events);


// 2.Remove [64, 'πŸ”Ά Yellow card'] from gameEvents

//gameEvents.delete(64, 'πŸ”Ά Yellow card')
gameEvents.delete(64) // only the key is nec.
/* console.log(...gameEvents) */

// 3.
console.log("An event happened, on average, every 9 minutes")

/* console.log(timesArray) */
 const aveArray=[];
 for (let [i,t] of timesArray.entries()) {
 let x= timesArray[i+1];
 /* console.log(i,t,x) */
 
 aveArray.push(Math.abs(t-x))
 //x=t
 } 
 //remove largest(first) value
/*  aveArray.shift() */
aveArray.pop()// remove last NaN value
 //console.log(aveArray)
 const reducer = (a,c) => a+=c
 const total = aveArray.reduce(reducer,0)
 
 const average = (total/aveArray.length)
 //console.log(total)
 //console.log(average)
// this is the average of time between actual events
  console.log(`An event happened, on average, every ${average} minutes`)
  
  
 //console.log(`An event happened, on average, every ${90/gameEvents.size} minutes`)
 
   const gameTime = [...gameEvents.keys()].pop()
  console.log(`An event happened, on average, every ${gameTime/gameEvents.size} minutes`)
  
//4.
for (const [t, e] of gameEvents) {
  t <= 45 ?
    console.log(`[FIRST HALf ${t}: ${e}`) :
    console.log(`[SECOND HALf ${t}: ${e}`);
}