Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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); 
}
 
PREVIOUS NEXT
Tagged: #JavaScript #function #multiply #set #numbers
ADD COMMENT
Topic
Name
6+1 =