How to check if a user’s answer is valid and re ask the question if it is not in javascript?

I am making a choose your own adventure story game and I am trying to take a user’s input and see whether they typed “n” or “y” and if they didn’t re-ask the question. Currently, when typing in something other than “y” or “n” the code works and re-asks the question.

However, when the user inputs “n” I want the code to update the variable choice to false and run code that tells the user they have failed and asks them if they would like to try again. This code does not run and instead continues the for loop.

I assume that this code also does not work when I try to update the variable restartGame.

I am super new to coding and this is the first proper code that I have made myself outside of my highschool class instruction so any help would be amazing. 🙂

Here is the code that I made:

let food = null;

let storyParts = ["You wake up in a dark hole and an alien approaches you. His name is Tony.",
"You board Tony's ship and you see a goblin in the corner. Tony tells you it doesn't bite.",
"The goblin is visibly shaken and bites off one of your fingers.", 
"Tony feels bad and offers you food.", 
"You eat the " + food]

let storyChoices = ["Do you board his ship? (Yes / No) ", 
"Do you pet the goblin? (Yes / No) ", 
"Spin for food? (Yes / No) ",
"Try again? (Yes / No) "]

let failStatements = ["Tony is disappointed in you. He was your father the whole time. Game over.", 
"Tony is upset. He asks you to leave. Game over."]

let foodOptions = ["pizza", "cupcake", "goblin soup", "finger", "pasta"]

function userInput(line) {
    while (1) {
        let answer = readLine(line);
        if (answer == "y") {
            break;
            return true;
        } else if (answer == "n") {
            break;
            return false;
        } else {
            println("Please type 'y' for yes and 'n' for no.");
        }
    }
}

for (let i = 0, len = storyParts.length; i < len; i++) {
    println(storyParts[i]);
    let choice = null;
    if (i == 0) {
        choice = userInput(storyChoices[0]);
    } else if (i == 1) {
        choice = userInput(storyChoices[1]);
    } else if (i == 4) {
        choice = userInput(storyChoices[2]);
    } else {
        
    }
    if (choice == false) {
        let restartGame = userInput(storyChoices[3]);
        if (restartGame == true) {
            i = -1;
            continue;
        } else if (restartGame == false) {
            i = len;
            break;
        } else {

        }
    }
}

The output is below. As you can see when I first type n it continues to the next part of the story when I want it to show the fail statement and ask if I would like to restart the story.

You wake up in a dark hole and an alien approaches you. His name is Tony.
Do you board his ship? (Yes / No) n
You board Tony's ship and you see a goblin in the corner. Tony tells you it doesn't bite.
Do you pet the goblin? (Yes / No) y
The goblin is visibly shaken and bites off one of your fingers.
Tony feels bad and offers you food.
You eat the null
Spin for food? (Yes / No) e
Please type 'y' for yes and 'n' for no.
Spin for food? (Yes / No) e
Please type 'y' for yes and 'n' for no.
Spin for food? (Yes / No) n