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

153
Vistas
Recursive function getting called repeatedly despite conditional logic

I'm new with Javascript so it's a little difficult to me to explain this properly.

I have this recursive function:

function askQuestion(question){
    let answer = prompt(question);
    if(answer === ""){
        alert("The answer is empty...");
        askQuestion(question);
    }
    return answer;
}

So, the problem: if the user response was empty and the function is called again, when the user indicates a non empty response the value of "answer" remains empty. I notice that the call stack of chrome is calling again the function even when the answer is empty. I can't understand why this is happening, but I suspect the problem is there. I tried to "reset" the value of answer to null, but then the value of answer stays as null.

📷 screenshot of the chrome inspect

Also, I'm not totally sure that this kind of functions are called recursive function... 😅

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

0

You can use a while loop instead, checking while the answer is empty to call the prompt(question)

while(answer === ""){
  answer =  prompt(question);
}
return answer;
about 4 years ago · Juan Pablo Isaza Denunciar

0

you should return the function result

const askQuestion = question => {
  let answer = prompt(question);
  if(answer === '') {
    alert("The answer is empty...");
    return askQuestion(question);
  }
  return answer;
}
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