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

158
Views
RangeError no capturado: se excedió el tamaño máximo de la pila de llamadas, ¿recurrencia con números aleatorios?

Así que sé que aparece el error máximo de pila de llamadas cuando se produce un bucle infinito, por recursión. Pero tengo este extraño problema. Todo en el código funciona bien, hasta que agregue estas 2 líneas comentadas en randomRow y randomCol. ¿Es posible crear un bucle infinito con números aleatorios o me estoy perdiendo algo?

 function insertNumbers(freeRCPos, grid, fields, num) { //base case if (fields.length === 0) return grid; let randomRow = Math.floor(Math.random() * maxRows); let randomCol = Math.floor(Math.random() * maxRows); // if (grid[randomRow][randomCol].value !== undefined) // return insertNumbers(freeRCPos, grid, fields, num); let newFields = []; //there are in total 9 fields so it has to go through all of them for (let field of fields) { //check if its already in field if (field.includes(JSON.stringify([randomRow, randomCol]))) { let res = freeRCPos.map((element: any) => element.map((pos: any) => pos === JSON.stringify([randomRow, randomCol]) ? true : false ) ); if ( res.filter((el: any) => (el.includes(true) ? true : false)).length > 0 ) { grid[randomRow][randomCol].value = num; newFields= fields.filter( (_: any, fieldId: number) => fields.indexOf(field) !== fieldId ); //return with modified fields and others to be able to complete base case return insertNumbers( freeRCPos.map((field: any) => field.map((pos: string) => pos?.startsWith("[" + randomRow) || pos?.endsWith(randomCol + "]") ? null : pos ) ), grid, newFields, num, ); //else return same } else { return insertNumbers(freeRCPos, grid, fields, num); } //else return same } else { return insertNumbers(freeRCPos, grid, fields, num); } } return grid; }

Tal vez he puesto mucho código innecesario aquí, así que si necesito acortarlo, solo pregunte. Agradezco la ayuda.

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

0

Si los números aleatorios son los índices existentes en grid (= if grid[randomRow][randomCol].value !== undefined es verdadero), nunca pasará la parte comentada y seguirá llamándose a sí mismo con los mismos valores. Debe actualizar algo antes de llamar a la función de forma recursiva y acercarse al caso base, que es fields.length === 0 .

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!