For the code below I wonder why the the last "0" after "(arr[i] || 0)" is needed?
I aknowledge that without this "0" the code doesnt run as intended, but I do not actually get what that last "0" after "," makes it work.
Could someone explain it?
function addTogether (...arrays) {
let result = [];
let arrayLength = Math.max(...arrays.map(array => array.length));
for (let i = 0; i < arrayLength; i++) {
result.push(arrays.reduce((sum, arr) => sum + (arr[i] || 0), 0));
}
return result
}
test(addTogether([1, 2, 3], [4, 5], [6]), [11, 7, 3]);