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

205
Vistas
How to get the next element in an array with recursion?

I want to loop through an array with a recursive function, but I'm kinda stuck on how to do it.

What I want to achieve is this:

  1. Get the first two arrays in numbersArray.
  2. Get the first numbers in both arrays and add these together and then divide by 10.
  3. Push the outcome into sumOfNumbers.
  4. Get the next set of numbers and in numbersArray and repeat until all the numbers have been calculated.
  5. Get the next set of two arrays in numbersArray and repeat.

This is what I have now. It only gets the first element of an array:

function unitTest(block1, block2, result = []) {

let sumOfNumbers = []

let numbersArray = [
    [9, 2, 3, 4, 5, 6, 7, 8, 9, 0],
    [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
    [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
    [9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
    [5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
    [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
    [9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
    [5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
    [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
    [9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
    [5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
    [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
    [9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
]

block1 = numbersArray[0][0]
block2 = numbersArray[1][0]

sumOfNumbers = (block1 + block2 ) % 10

result.push(sumOfNumbers)

console.log(sumOfNumbers)

console.log(result)

if (result.length == 10) {
  return result  
}else{
    // unitTest()
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

In recursion you need a base case and a recursive case. I put the whole array as a parameter and an index to know what item in the array we are processing. The stop recursion occurs when that index is greater than array. In recursive cases, we take 2 arrays and do the operations. Then, we call recursive the 2 next blocks

function unitTest(numbersArray, arrayIndex, result = []) {
    if (arrayIndex + 1 < numbersArray.length) {
      var block1 = numbersArray[arrayIndex]
    var block2 = numbersArray[arrayIndex + 1]

        for (var i = 0; i < block1.length; i++){
      sumOfNumbers = (block1[i] + block2[i]) % 10;

      result.push(sumOfNumbers);

      console.log(sumOfNumbers);
    }
    
    unitTest(numbersArray, arrayIndex + 2, result);
  } else {
      console.log(result);
  }
}

let sumOfNumbers = []

let numbersArray = [
    [9, 2, 3, 4, 5, 6, 7, 8, 9, 0],
    [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
    [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
    [9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
    [5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
    [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
    [9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
    [5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
    [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
    [9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
    [5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
    [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
    [9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
]

unitTest(numbersArray, 0)
about 4 years ago · Juan Pablo Isaza Denunciar

0

from what I understand, you are trying to compute the sum % 10 of each element of the array 0-1, 2-3, 4-5, ...

The usage of the recursion here is not necessary and a simple loop will do the job.

Here is the solution I could suggest

const numbersArray = [
  [9, 2, 3, 4, 5, 6, 7, 8, 9, 0],
  [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
  [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
  [9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
  [5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
  [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
  [9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
  [5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
  [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
  [9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 7],
  [5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 4],
  [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
  [9, 4, 3, 2, 1, 0, 9, 8, 7, 6],
  [1, 2, 3, 4, 5, 6, 7, 8, 9, 7]
];

const results = [];
for (let i = 0; i < numbersArray.length - 1; i += 2) {
  const result = numbersArray[i].map((value, index) => {
    return (value + numbersArray[i + 1][index]) % 10;
  });
  results.push(result);
}

console.log(results);

The results constant is of type number[][] which contains the arrays of the computation for each pair of the original array.

Hope that will help you :)

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