getAverageValue

getAverageValue

  • (collection: Numbers[]) => number | undefined
  1. Return the average

    Returns undefined for empty collections.

    getAverageValue([1, 2, 3, 4]) // is 2.5
    getAverageValue([]) // is undefined
    getAverageValue([1, 2, 3, 4]) // is 2.5
    getAverageValue([]) // is undefined
    
    getAverageValue([1, 2, 3, 4]) // is 2.5
    getAverageValue([]) // is undefined
    getAverageValue([1, 2, 3, 4]) // is 2.5
    getAverageValue([]) // is undefined
    
  2. Sometimes we need the average. Below, we have some test scores. The class earns a pizza party if they average a B. Let's find the average.

    const scores = {
      jen: 72,
      mike: 88,
      luke: 93,
    }
    const getRoundedAvg = compose([getAverageValue, roundToNearest('0.1')])
    const roundedAvg = getRoundedAvg(scores)
    console.log(roundedAvg) // is 84.3
    type Scores = Record<string, number>
    const scores: Scores = {
      jen: 72,
      mike: 88,
      luke: 93,
    }
    const getRoundedAvg = compose([getAverageValue<Scores>, roundToNearest('0.1')])
    const roundedAvg = getRoundedAvg(scores)
    console.log(roundedAvg) // is 84.3
    
    const scores = {
      jen: 72,
      mike: 88,
      luke: 93,
    }
    const getRoundedAvg = compose([
      getAverageValue,
      roundToNearest('0.1'),
    ])
    const roundedAvg = getRoundedAvg(scores)
    console.log(roundedAvg) // is 84.3
    type Scores = Record<string, number>
    const scores: Scores = {
      jen: 72,
      mike: 88,
      luke: 93,
    }
    const getRoundedAvg = compose([
      getAverageValue<Scores>,
      roundToNearest('0.1'),
    ])
    const roundedAvg = getRoundedAvg(scores)
    console.log(roundedAvg) // is 84.3