-1

I have a map of an events of a football game, who looks like this:

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

And i need to take every key (that is the minute in the game when an event happened) and calculate the average of minutes when an event happens and log to the console a string like this: 'an event happened on average every 9 minutes'

I don't have a clear idea of what to do to solve the problem

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
  • 2
    Are you looking for the average of _any_ event, or the average calculations of _each_ event type? Get familiar with [how to access and process objects or arrays](/q/11922383/4642212), and use the static and instance methods of [`Map`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map#instance_methods), [`Object`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Jan 30 '23 at 19:24

1 Answers1

1

The average time between events is the average value of the intervals between each event. This can be calculated by running a for loop, adding each time interval between the current and the next event, and dividing by the number of intervals at the end.

This can be made a little faster by observing that when summing successive differences, the middle terms cancel out. Therefore the sum of the intervals is just the difference between the final time and the initial time.

Note: Think twice what to use as the denominator.

Fractalism
  • 1,231
  • 3
  • 12
  • Seems like the OP's question is a homework assignment, no? Please don't answer someone's assignment for them, especially when they haven't shown any work or asked any specific question. – David Makogon Jan 30 '23 at 19:37
  • @DavidMakogon Sorry about that. I removed and code and left some textual guidance instead. – Fractalism Jan 30 '23 at 19:42