Skip to main content

Function: average()

average(iterable: IterableResolvable <NumberResolvable>): number | null

Consumes the iterable and returns the average value of all the elements. If the iterable is empty, it returns null.

Parameters

ParameterTypeDescription
iterableIterableResolvable <NumberResolvable>The iterator to calculate the average of.

Returns

number | null

The average of the sequence of numbers, or null if the sequence is empty or contains only non-number values.

Example

import { average } from '@sapphire/iterator-utilities';

const iterable = [1, 2, 3, 4, 5];
console.log(average(iterable));
// Output: 3

Remarks

This function consumes the entire iterator.

Source

projects/utilities/packages/iterator-utilities/src/lib/average.ts:24