Home > other >  Recursive function getting called repeatedly despite conditional logic
Recursive function getting called repeatedly despite conditional logic

Time:03-15

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.

  • Related