You can use reduce to finish the multiply action.
function multiply(arr) {
if (arr.length <= 0) return 1;
return arr.reduce((result, number) => {
return result * number;
}, 1);
}
Another thing about recursion is tail optimization, it can reduce the usage of call stack to minimize space complexity.
You can use
reduceto finish the multiply action.function multiply(arr) { if (arr.length <= 0) return 1; return arr.reduce((result, number) => { return result * number; }, 1); }Another thing about recursion is tail optimization, it can reduce the usage of call stack to minimize space complexity.