Answers for "a JavaScript function to multiply a set of numbers"

1

a JavaScript function to multiply a set of numbers

function multiply(...myArray) {
    let result = 1;
    for (let i = 0; i < myArray.length; ++i) { 
      result *= myArray[i]; 
    }
    console.log("The product is: " + result); 
}

// For example,
// multiply(1,2,3,4);
// => 24

// The '...' is a rest operator that converts 
// any parameter in the function to an array

// Here is a shorter way of doing the same thing.
function multiply(...myArray) {
    let result = 1;
    myArray.forEach(e => {
        result *= e;
    });
    console.log("The product is: " + result); 
}
Posted by: Guest on March-03-2022

Code answers related to "a JavaScript function to multiply a set of numbers"

Code answers related to "Javascript"

Browse Popular Code Answers by Language