skip to Main Content

I need help with my React Native app. I need to calculate the average value between each element of the array. The logic is as follows, if there are 4 elements with values 3,5,6,9, then you need to calculate the average value between 3 and 5 and divide by 1, between 3, 5, 6 and divide by 2, etc. The first value is counted as the initial value, so it is not taken into account.

I would appreciate any help.

    return (
        <View style={Styles.sectionContainer}>
            {readings.map(
                ({
                    id,
                    date,
                    volume,
                }) => {
                    return (
                        <View key={id} style={Styles.card}>
                            <Text
                                style={[
                                    Styles.sectionHeading,
                                    Styles.darkAppText,
                                    { fontWeight: "bold" },
                                ]}
                            >
                                average: {average}
                            </Text>
                        </View>
                    );
                }
            )}
        </View>
    );
}

I was trying to do this inside map function, but it gives average between 2 last elements:

if (id == 1) {
  average = 0;
} else if (id > 1) {
  for (i = 1; i < readings.length; i++) {
    average = (readings[i].volume readings[i - 1].volume) / i;
  }
}

2

Answers


  1. The below functions compute the average values according to what you’ve described and returns them as an Array:

    const data = [3,5,6,9];
    
    function avg(first, ...rest) {
      const v = rest.reduce((a, b) => a + b, first);
      return v / rest.length;
    }
    
    function getAvgAll(values) {
      const results = [];
      for (let i = 1; i < values.length; i += 1) {
        results.push(avg(...values.slice(0, i + 1)));
      }
      return results;
    }
    
    console.log(getAvgAll(data));
    Login or Signup to reply.
  2. Your problem has nothing to do with React Native rather its simply a JS job.here is the code

    function calculateAverageValues(arr) {
      if (arr.length < 2) {
        return []; // If there are less than 2 items,return empty
      }
    
      const outputArray = [];
      let sum = arr[0]; // Initialize the sum with the first item as the 
      initial value.
    
      for (let i = 1; i < arr.length; i++) {
        sum += arr[i]; // Add the current element to the sum.
        const average = sum / i; // Calculate the average value.
        outputArray.push(average); // Push the average value to the output 
      array.
      }
    
      return outputArray;
    }
    

    Example usage:

    const inputArray = [3, 5, 6, 9];
    const result = calculateAverageValues(inputArray);
    console.log(result); // Output: [4, 4.666666666666667, 5.666666666666667]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search