Query
I am facing an issue with a specific array and reducer function. The array in question is as follows:
let oddsArray = [undefined, undefined, 5, 5]
Accompanied by this reducer function:
const reducer = (accumulator, currentValue) => accumulator + currentValue
Upon calling console.log, I encounter the following output:
console.log(oddsArray.reduce(reducer, 1), 'oddsArray') /// NAN
This only changes when the array excludes any undefined values:
[5, 5, 5, 5] /// 20
Desired Outcome
My objective is to sum up all numbers within the array even if there are undefined elements present. Therefore, the initial total of the array should have been:
let oddsArray = [undefined, undefined, 5, 5] // 10
Is there a method to achieve this?
Thank you!