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

95
Views
Qn-imprime y escribe todos los elementos de la matriz usando recursividad en JavaScript porque soy nuevo en el concepto de recursividad y no pude resolver el problema

Aquí está mi código y sé que hay algún error en mi código, pero soy nuevo en el concepto de recursividad. Creo que hay un problema porque traté de implementar el mismo código que el lenguaje C. Quiero que mi respuesta lea e imprima una matriz usando la recursividad. Y traté de convertir el código c en javascript, el código c ++ en javascript y, por último, el código python en javascript.

 var a = [1, 2, 3]; var l = a.length; var num = 0; function printArr(a, i, l){ for(var i=0; i<l; i++){ num = a[i]; } if(i>=l){ return 0; printArr(a, i+1, l); //<I think in this section I'm having a problem.> } } printArr(a, 0, l); console.log(num);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

No hay necesidad de eso para bucle.

Su llamada printArr debe estar fuera de la declaración if .

 var a = [1, 2, 3]; var l = a.length; //var num = 0; // not needed function printArr(a, i, l) { // Base condition - to break out of recursion // check if index is out of array bounds, if yes return if (i >= l) { return; } // if not, print the array element at that index console.log(a[i]); // call the recursive function with next consecutive index printArr(a, i + 1, l); } printArr(a, 0, l);

about 4 years ago · Juan Pablo Isaza Report

0

Otro método nos permitiría evitar jugar con los índices, desestructurar la matriz de entrada y verificar si el primer elemento está vacío:

 const printArr = ([x, ...xs]) => { if (x == null) return null console .log (x) return printArr (xs) } printArr ([1, 2, 3])

Personalmente, preferiría escribir esto como una sola expresión, así:

 const printArr = ([x, ...xs]) => x == null ? null : (console .log (x), printArr (xs))
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!