Answers for "multiply elements of array javascript"

2

multiply arrays javascript

var sum = 0;
for(var i=0; i< arr1.length; i++) {
    sum += arr1[i]*arr2[i];
}
Posted by: Guest on August-21-2021
-1

multi dimensional array javascript

//2D array
let activities = [
    ['Work', 9],
    ['Eat', 1],
    ['Commute', 2],
    ['Play Game', 1],
    ['Sleep', 7]
];
Posted by: Guest on November-12-2020
-2

multiply all numbers in an array javascript

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`);
}
Posted by: Guest on March-19-2021

Code answers related to "multiply elements of array javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language