I'm never to NodeJS and therefore struggling with a recursive multiplication function. The problem seems to be that in the recursion the input parameters are an array which is then assigned to the first value instead of value by value. Here's the code
const multiply = (factor1, ...factors) => {
if (factors.length > 1){
return Math.round(factor1 * multiply(factors) * scale) / scale;
}
const factor2 = factors[0];
return Math.round(factor1 * factor2 * scale) / scale;
}
so calling with: multiply(1,1,1,1)
leads to:
- iteration:
factor1: 1, factors: [1,1,1]
- iteration:
factor1: [1,1,1], factors: Array(0)
how can I achieve to have in the second iteration factor1: 1, factors: [1,1]
, without checking if factor1 is an array?