Home > Blockchain >  How to multiply all arguments together inside an array and return the product
How to multiply all arguments together inside an array and return the product

Time:10-16

How to multiply all arguments together inside an array and return the product

  • if no arguments are passed in, return 0
  • if one argument is passed in, just return it
  • I'm using JavaScript

This is what I tried but I really don't know what to do. Appreciate any help!


function multiplyArguments() {

    let product = 1;

   for (let i = 0; i < arguments.length; i  )
   {
    product = product * arguments[i];
   }
  return product;
} 

CodePudding user response:

Your code is fine. Just add an if to handle the 0 args condition.

Note: you should prefer using rest parameter - you can see the differences here.

function multiplyArguments(...args) {
  if(args.length === 0) return 0;

  let product = 1;

  for (let i = 0; i < args.length; i  ) {
    product = product * args[i];
  }
  
  return product;
}

console.log(multiplyArguments());
console.log(multiplyArguments(5));
console.log(multiplyArguments(5, 2));
console.log(multiplyArguments(5, 2, 4));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

You can also use Array.reduce():

const multiplyArguments = (...args) =>
  args.length
    ? args.reduce((prod, n) => prod * n)
    : 0;

console.log(multiplyArguments());
console.log(multiplyArguments(5));
console.log(multiplyArguments(5, 2));
console.log(multiplyArguments(5, 2, 4));
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Using reduce and Rest Parameters

function multiplyAllArgs(...args) {
  if(args.length === 0) return 0; 
  return args.reduce((arg, item) => (arg*item), 1);
}

console.log(multiplyAllArgs());
console.log(multiplyAllArgs(2));
console.log(multiplyAllArgs(2, 2));
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

How to multiply all arguments together inside an array and return the product

  • if no arguments are passed in, return 0
  • if one argument is passed in, just return it
  • I'm using JavaScript

These are somewhat strange requirements. The neutral element of multiplication is 1, not 0, so it would make much more sense to return 1 in case the collection is empty.

Typically, when you want to "reduce" elements of a collection into one value, the method to use for that is the aptly named Array.prototype.reduce:

const multiplyArguments = (...nums) => nums.reduce((acc, el) => acc * el, 1);

console.log(multiplyArguments());        //  1
console.log(multiplyArguments(5));       //  5
console.log(multiplyArguments(5, 2));    // 10
console.log(multiplyArguments(5, 2, 4)); // 40
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

As you can see, this is absolutely trivial if we want to return the neutral element for an empty collection, which is IMO the right way to go.

Modifying this to return 0 is simple, though: we can either conditionally pass a different neutral element, or conditionally bypass the reduce altogether.

Here we pass a different neutral element:

const multiplyArguments = (...nums) => 
    nums.reduce(
        (acc, el) => acc * el,
        nums.length === 0 ? 0 : 1
    );

console.log(multiplyArguments());        //  0
console.log(multiplyArguments(5));       //  5
console.log(multiplyArguments(5, 2));    // 10
console.log(multiplyArguments(5, 2, 4)); // 40
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

We can be more clever and use the fact that 0 is falsey and all other numbers are truthy:

const multiplyArguments = (...nums) => 
    nums.reduce(
        (acc, el) => acc * el,
        nums.length ? 1 : 0
    );

console.log(multiplyArguments());        //  0
console.log(multiplyArguments(5));       //  5
console.log(multiplyArguments(5, 2));    // 10
console.log(multiplyArguments(5, 2, 4)); // 40
<iframe name="sif6" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

We can be even more clever and use the fact that the numeric coercion of true is 1 and false is 0:

const multiplyArguments = (...nums) => 
    nums.reduce(
        (acc, el) => acc * el,
         !!nums.length
    );

console.log(multiplyArguments());        //  0
console.log(multiplyArguments(5));       //  5
console.log(multiplyArguments(5, 2));    // 10
console.log(multiplyArguments(5, 2, 4)); // 40
<iframe name="sif7" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Note that "being clever" sounds cool but is actually not a good idea! In particular, debugging code is much harder than writing it in the first place, which means that if you write code as clever as you can, you are by definition not smart enough to debug it!

Or, we just skip the reduce altogether:

function multiplyArguments(...nums) {
    if (!nums.length) { return 0; }
    return nums.reduce(
        (acc, el) => acc * el,
         !!nums.length
    );
}

console.log(multiplyArguments());        //  0
console.log(multiplyArguments(5));       //  5
console.log(multiplyArguments(5, 2));    // 10
console.log(multiplyArguments(5, 2, 4)); // 40
<iframe name="sif8" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Note that arguments is deprecated and should not be used.

  • Related