Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
recursive function that calls recursion inside for loop in javascript

I have a class for a Street which is just a line with a beginning point and an end point, inside this same class I have a function called checkIntersections which :

  • checks if this Street intersects with a given Street and its children which are also of type Street.
  • returns true at the first found intersection
  • returns false or undefined if no recursive intersections were found

I decided to make checkIntersections a recursive function but I think I'm going about it wrong because nested intersections between streets are not being detected.

  checkIntersections(street){

if(intersects(this.beginning, this.end, street.beginning, street.end)){
  return true;
}
else{
  for(var i = 0 , count = street.children.length ; i < count ; i++){
    this.checkIntersections(street.children[i]);  
  }
}

}

Is there anything that I should be attentive to while making recursive calls inside a for loop in javascript ? The output of the function is always undefined.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your code is not using the value returned from the recursive call -- it just ignores it and just continues with the next recursive call. Instead it should detect a success from a recursive call and then exit immediately, returning that success to its own caller.

checkIntersections(street) {
    if (intersects(this.beginning, this.end, street.beginning, street.end)) {
        return true;
    } else {
        for (let child of street.children) { // Use modern for..of loop
            let success = this.checkIntersections(child);
            if (success) return true; // bubble up the happy result!
        }
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

It should be something like

checkIntersections(street) {
  if (intersects(this.beginning, this.end, street.beginning, street.end)) {
    return true;
  } else if (street.children.length) {
    return street.children.some( checkIntersections );
  }
  return false;
}

  • you need to return the recursive call to checkIntersections
  • it would be better to use .some for the children as it will do a early exit
  • you need to provide a default return in case there are no children and the current intersection failed.
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda