avg
Find the average of an array of numbers
Definition
number avg(array[number] $elements)
Returns the arithmetic mean of the elements in the provided array.
Parameters
array[number] $elements
The numbers to find the arithmetic mean of.
Returns
number
The arithmetic mean of the elements in $elements
.
Examples
{
numAvg: avg([`1`, `2`, `3`]),
decimalStringAvg: avg(['1.5', `2.1`, `2.9`]),
boolAvg: avg([`true`, `false`]),
nullAvg: avg([`1`, `2`, `null`]),
emptyAvg: avg([]), // Error
textAvg: avg(['a', 'b']), // Error
noArrayAvg: avg(`5`) // Error
}
{
"numAvg": 2.0,
"decimalAvg": 2.1666666666666665,
"boolAvg": 0.5,
"nullAvg": 1.0
}
Remarks
null
, true
and false
are all converted to their numerical values; no errors are raised if they appear in $elements
.
Updated about 1 year ago