Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda