Skip to content

How to calculate the average amount for 10 days (24 elements for each day) out of an array with 240 elements in one function #758

Answered by e-rmkt
TobiasSteinhagen asked this question in Web
Discussion options

You must be logged in to vote

We found a possible solution, that works, but we are unsure if this is considered "good" or best practice:

const dailyAverages = [];

  precipitation_probability.forEach((value, index) => {
    const dayIndex = Math.floor(index / 24); 
    if (!dailyAverages[dayIndex]) {
      dailyAverages[dayIndex] = []; 
    }
    dailyAverages[dayIndex].push(value);
  });

  const calculatedAverages = dailyAverages.map((dayData) => {
    const sum = dayData.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
    return Math.round(sum / dayData.length);
  });

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by TobiasSteinhagen
Comment options

You must be logged in to vote
1 reply
@moonwave99
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Web
Labels
None yet
4 participants