Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda