freeCodeCamp/curriculum/challenges/espanol/02-javascript-algorithms-an.../basic-javascript/nesting-for-loops.md

1.7 KiB

id title challengeType videoUrl forumTopicId dashedName
56533eb9ac21ba0edf2244e1 Anida bucles "for" 1 https://scrimba.com/c/cRn6GHM 18248 nesting-for-loops

--description--

Si tienes un arreglo multidimensional, puedes utilizar la misma lógica que aprendimos anteriormente para recorrer tanto el arreglo como cualquier sub-arreglo. Aquí hay un ejemplo:

const arr = [
  [1, 2], [3, 4], [5, 6]
];

for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr[i].length; j++) {
    console.log(arr[i][j]);
  }
}

Esto imprime cada sub-elemento dentro de arr uno a la vez. Ten en cuenta que para el bucle interior, estamos comprobando el .length de arr[i], ya que arr[i] es en sí mismo es un arreglo.

--instructions--

Modifica la función multiplyAll para que devuelva el producto de todos los números dentro de los sub-arreglos de arr.

--hints--

multiplyAll([[1], [2], [3]]) debe devolver 6

assert(multiplyAll([[1], [2], [3]]) === 6);

multiplyAll([[1, 2], [3, 4], [5, 6, 7]]) debe devolver 5040

assert(
  multiplyAll([
    [1, 2],
    [3, 4],
    [5, 6, 7]
  ]) === 5040
);

multiplyAll([[5, 1], [0.2, 4, 0.5], [3, 9]]) debe devolver 54

assert(
  multiplyAll([
    [5, 1],
    [0.2, 4, 0.5],
    [3, 9]
  ]) === 54
);

--seed--

--seed-contents--

function multiplyAll(arr) {
  let product = 1;
  // Only change code below this line

  // Only change code above this line
  return product;
}

multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);

--solutions--

function multiplyAll(arr) {
  let product = 1;
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; j++) {
      product *= arr[i][j];
    }
  }
  return product;
}