The below javascript reduce function produce wrong sum , is it?
Kindly help me to understand why?
What is the right way to get the correct sum?
const array1 = [2.01,2.01,2.01,2.01,2.01,2.01,2.01,2.01,2.01,1.91];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 2.01,2.01,2.01,2.01,2.01,2.01,2.01,2.01,2.01,1.91
console.log(array1.reduce(reducer));
// 5 + 2.01,2.01,2.01,2.01,2.01,2.01,2.01,2.01,2.01,1.91
console.log(array1.reduce(reducer, 5));
Result :
19.999999999999996 24.999999999999996
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce