Sign in
Log inSign up

The Rest and Spread Operators

Emmy Leke's photo
Emmy Leke
·Jun 5, 2019

Hi. I've been trying to multiply all elements of an array using the Rest and Spread Operators. It seems like it is impossible when I really breakdown the process in my head. I just want to be sure either I'm the limit or the language is.

Here is something I tried using the Array.reduce method.

//Function that multiplies the array using the rest operator to take in args
function multiply(...args){
    Using the reduce function to multiply each element
    args.reduce((a,b)=> console.log(a*b));
}; 
//Variable pointing to the array
var list = [1,2,3,4,5];

//calling the function
multiply(...list);

NAN kept getting logged

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct