In the reduce method the following works:
let dolphins = [92,108,89]
dolphins = dolphins.reduce(function(a,b){
return a + b/dolphins.length},0)
note that inside the reduce function we have access to dolphins.
From the documentation we have:
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
Parameters
Parameter Description
function() Required.
A function to be run for each element in the array.
Reducer function parameters:
total Required.
The initialValue, or the previously returned value of the function.
currentValue Required.
The value of the current element.
currentIndex Optional.
The index of the current element.
arr Optional.
The array the current element belongs to.
What's the point of passing the array since the function already has access to it?
Via Active questions tagged javascript - Stack Overflow https://ift.tt/qv9MHlC
Comments
Post a Comment