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
-
Hello there, we want to calculate the average precipitation probability for each day, for ten days. We have an array with 240 elements and don't want to calculate it 10 times, like that:
We tried it like that:
But unfortunately, we receive just the first element of the new array when we call it. Here is our code and our PR
Thanks in advance, looking forward to your feedback! ✌️ |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
We found a possible solution, that works, but we are unsure if this is considered "good" or best practice:
|
Beta Was this translation helpful? Give feedback.
-
Nice solution! I came up with something similar: const dailyAverages = [];
for (let i = 0; i < precipitation_probability.length; i += 24) {
const oneDayData = precipitation_probability.slice(i, i + 24);
const dailyAverage = oneDayData.reduce((sum, value) => sum + value, 0) / 24;
dailyAverages.push(dailyAverage);
} |
Beta Was this translation helpful? Give feedback.
We found a possible solution, that works, but we are unsure if this is considered "good" or best practice: