0

Having a bit of trouble with the following task, which logs "1":

Create a function multiplyAll that takes an unknown number of integer arguments, multiplies them all together, and returns the result.

function multiplyAll(arr){
 let product = 1;
for (let i=0; i<arr.length; i++){
    product = product * arr[i];
}
  return product;
}

console.log(multiplyAll(9, 4, 5, 6, 7, 2, 1, 8, 3)) // should log: 362880
console.log(multiplyAll(5, 5, 5, 3)) // should log: 375
John Belknap
  • 15
  • 1
  • 4
  • `arr` is not an array. You pass multiple arguments to the function instead of an array. Change the declaration to `function multiplyAll(...arr)` – VLAZ May 11 '21 at 17:44

1 Answers1

1

this way:

function multiplyAll(...arr){
  return arr.reduce((m,v)=>m*v,1)
}

console.log(multiplyAll(9, 4, 5, 6, 7, 2, 1, 8, 3)) // should log: 362880
console.log(multiplyAll(5, 5, 5, 3)) // should log: 375
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40