I couldn't find an example here what I'm really looking for. I'd like to multiply all array elements, so if an array contains [1,2,3] the sum would be 1 2 3=6; So far I've got this code, but it returns undefined.
function multiply (array) {
var sum=1;
for (var i=0; i<array.length; i++) {
sum = sum * array[i];
}
return sum;
}
console.log(multiply[1,2,3]);
Could anyone please explain, what am I missing here?
The cause is already known. Here's an alternative - using Array.reduce
for your method:
console.log( [1, 2, 3].reduce( (a, b) => a * b ) ); console.log( Array.from( {length: 20} ) .map( (v, i) => i + 1 ) .reduce( (a,b) => a * b ) .toLocaleString()); // for empty arrays, use some initial value const arr = []; if (arr.reduce( (a, b) => a * b, -1 ) === -1) { console.error(`The given array ${arr} is empty`); }
The alternative way to use Array.reduce
should have the initial value set to 1 or else our function will return 0 no matter what.
[1, 2, 3].reduce((a, b)=> a*b, 1)
per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
您没有将 multiply 作为函数调用:
multiply([1,2,3]);
This is the simplest way to do any arithmetic operation on array
myMathArray = [1,2,3,4,5] let myValue = myMathArray.reduce((a,b)=> a*b) console.log(myValue)
If you want to multiply some consecutive numbers like 1,2,3 then, then apply this code and enter the number in the console (arr)
let array = [];
function factorisation(arr) {
for (let j = arr; j > 0; j--) {
array.push(j);
}
return multiply();
}
function multiply() {
var sum = 1;
for (var i = 0; i < array.length; i++) {
sum = sum * array[i];
}
return sum;
}
console.log(factorisation(5));
//5*4*3*2*1 = 120
Using Lodash >=4.7:
_.reduce(array, _.multiply)
or, using all three arguments:
_.reduce(array, _.multiply, 1)
The reduce() method executes a provided function for each value of the array and reduces the array to a single value
const arr = [1,2,3]; const sum = arr.reduce((prevValue,curValue) => { return prevValue * curValue },1); console.log(sum);
prevValue is the initial value and it's equal 1 in this example
curValue is the value of the current element in array
This can be simplified using recursion...
function productOfArray(arr) {
let sum = 1;
function sumHelper(nums) {
if(nums.length === 0)
return 0;
sum = sum * nums[nums.length-1];
nums.length--;
sumHelper(nums);
}
sumHelper(arr);
return sum;
}
console.log(productOfArray([1,2,3]));
If you want to use recursion
const productOfArray = (arr) => { if (arr.length === 0) { return 1; } return arr[0] * productOfArray(arr.slice(1)); } console.log(productOfArray([1,2,3,10])); // 60
multiply all element array use below example:
const arr=[1,2,3,2]
use reduce();
const multiply=arr.reduce((preValue,nextValue)=>preValue *nextValue )
console.log(multiply)//12
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.