Reputation: 13
Write a function, multiply, that takes any number of arguments and multiplies them all together.
If there is only one number, it will return that number.
If no numbers are passed in, it will return 0.
For example:
multiply()
returns 0
multiply(1)
returns 1
multiply(1, 2)
returns 2
multiply(1, 2, 3)
returns 6
multiply(1, 2, 3, 4)
returns 24
function multiply(...a) {
if (!a){
return 0;
} else if (a === Number){
return a;
} else {
return a.reduce((current, previous)=>{return previous*current});
}
}
multiply(1,2,3);
Upvotes: 1
Views: 2375
Reputation: 2745
After ...
operation You will always have an array in this case, so You just must use:
if(!a.length){
instead of if(!a){
;
Also, You don't need return
in reduce()
callback's arrow function in this case. You can change it on:
(current, previous) => previous*current
Upvotes: 5