I want to sum the total of a multi dimensional array in JavaScript however it doesn't provide the right value because the nested array concatenates with the sum. Below is my code:
var arr = [1, 2, 3, [4, 3], [10, 50], 98, 100];
function recursion(array, length = 0) {
if (array.length === length) return 0;
if (Array.isArray(array[length])) {
recursion(array[length]);
}
console.log(array[length]);
return array[length] + recursion(array, length + 1);
}
console.log(recursion(arr));
The error can be seen in the below screenshot which is the console of google chrome.

concatandreduce:[].concat(...arr).reduce((a, b) => a + b)javascriptdoes support default parameters.