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

500
Visualizações
Interleave two arrays (card decks) using recursion... I was able to solve it without recursion but want to know what I am doing wrong

I am attempting to write a function that takes two arrays as inputs, representing the top and bottom halves of a deck of cards, and shuffles them together using recursion. I am attempting return a single array containing the elements from both input arrays interleaved, like so:

  • the first element should be the first element of the first input array
  • the second element should be the first element of the second input array,
  • the third element should be the second element of the first input array,
  • the fourth element should be the second element of the second array,

...etc.

I want to append any remaining elements to the end of the array.

This is how I solved it without recursion:

function shuffleCards(topHalf, bottomHalf) {
  let returnArray = [];
  for (let i = 0; i < Math.max(topHalf.length, bottomHalf.length); i++) {
    if (topHalf[i]) {
      returnArray.push(topHalf[i]);
    }
    if (bottomHalf[i]) {
      returnArray.push(bottomHalf[i]);
    }
  }
  return returnArray
}

and this is my attempt with recursion:

function shuffleCards(topHalf, bottomHalf) {
  let results = [];
  if (topHalf.length) {
    results.push(topHalf[0]
    } 
  if (bottomHalf.length) {
      results.push(bottomHalf[0])
    }
  return results.concat(shuffleCards(topHalf.slice(1), bottomHalf.slice(1)));
}

I keep getting the syntax error "missing ) after argument list" but I am fairly certain all of the parenthesis are in the write place.

Any tips?

Thanks!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Beside missing parenthesis, you could take only the first item from the first half and call the function with swapped arrays.

function shuffleCards([value, ...topHalf], bottomHalf) {
    return value === undefined
        ? [...bottomHalf]
        : [value, ...shuffleCards(bottomHalf, topHalf)];
}

console.log(...shuffleCards([1, 2, 3, 4], [5, 6, 7, 8]));

about 4 years ago · Juan Pablo Isaza Relatório

0

function shuffleCards(topHalf, bottomHalf,results = []) {
    if(topHalf.length===0&&bottomHalf.length===0)return results;
    if (topHalf.length!==0) {
    results.push(topHalf[0])
    } 
    if (bottomHalf.length!==0) {
      results.push(bottomHalf[0])
    }
    return shuffleCards(topHalf.slice(1), bottomHalf.slice(1),results);
}
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