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

158
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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