Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

504
Views
Intercalar dos matrices (mazos de cartas) usando recursividad... Pude resolverlo sin recursividad pero quiero saber qué estoy haciendo mal

Estoy intentando escribir una función que tome dos matrices como entradas, que representen las mitades superior e inferior de una baraja de cartas, y las mezcle usando recursividad. Estoy intentando devolver una sola matriz que contiene los elementos de ambas matrices de entrada intercaladas, así:

  • el primer elemento debe ser el primer elemento de la primera matriz de entrada
  • el segundo elemento debe ser el primer elemento de la segunda matriz de entrada,
  • el tercer elemento debe ser el segundo elemento de la primera matriz de entrada,
  • el cuarto elemento debe ser el segundo elemento de la segunda matriz,

...etc.

Quiero agregar los elementos restantes al final de la matriz.

Así es como lo resolví sin recursividad:

 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 }

y este es mi intento con la recursividad:

 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))); }

Sigo recibiendo el error de sintaxis "falta) después de la lista de argumentos", pero estoy bastante seguro de que todos los paréntesis están en el lugar de escritura.

¿Algun consejo?

¡Gracias!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Además de los paréntesis faltantes, podría tomar solo el primer elemento de la primera mitad y llamar a la función con matrices intercambiadas.

 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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!