I want to use Functional Programming, JavaScript, chaining, and no external library to reduce an array of objects to the average of the grade of the element with the condition that enrolled >1.
So far I’m able to calculate the sum under the requested condition
But how can I calculate the average? ( Note that I want to use Functional Programming, JavaScript, chaining, and no external library)
Here is what I have come so far …
let enrollment= [
{enrolled: 2, grade:100},
{enrolled: 2, grade:80},
{enrolled: 1, grade:89}
]
const sum = (accumulator, curr) => accumulator + curr.grade;
const average = (total,arr) => total/arr.length
enrollment
.filter( elem => elem.enrolled >1)
.reduce(sum,0)
Now how do I chain the average function to the above block of code?
Thanks in advance