Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
Javascript Multiplying Within an Array

I have an array with multiple arrays inside. I would like to multiply the contents of each array.

Here is my code--currently not getting anything in console. The desired output is 3 numbers-- the result of multiplication of the three arrays

var arr = [
  [1, 2, 3],
  [3, 4],
  [7, 8, 9]
]
var mult = 1
for (i = 0; i < arr.length; i++) {
  for (j = 0; j < arr.length; j++) {
    mult *= arr[i][j]
  }
  console.log(mult)
}

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could map the result of the nested multiplying.

const
    multiply = (a, b) => a * b,
    array = [[1, 2, 3], [3, 4], [7, 8, 9]],
    result = array.map(a => a.reduce(multiply, 1));

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You should iterate over rows. So you need to change inner loop on this :

for (j = 0; j < arr[i].length; j++)

const array = [
  [1, 2, 3],
  [3, 4],
  [7, 8, 9]
];

for (i = 0; i < array.length; i++) {
let result = 1;
  for (j = 0; j < array[i].length; j++) {
    result *= array[i][j]
  }
  console.log(result);
}

about 4 years ago · Juan Pablo Isaza Report

0

You can try this, it is easier using map and reduce functions. arr.map(array => array.reduce(arrayMult, 1)) is the multiplication of each inner array, like [ 6, 12, 504 ], and the last reduce is to multiply these values.

var arr = [[1, 2, 3],[3, 4],[7, 8, 9]];
const arrayMult = (prev, curr) => prev * curr;
const total = arr.map(array => array.reduce(arrayMult, 1)).reduce(arrayMult, 1);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!