Script closes when user input is below minimum value instead of prompting again

nodejs

#1

I’m making a game where the minimum number of players is 5.

If the user inputs a value less than 5, the user should told that the minimum is 5, then prompted again.

My code works when the user input is >= 5, but closes immediately when input is below 5 instead of doing what it’s supposed to.

const readline = require('readline');

var players = []

var rl = readline.createInterface(process.stdin, process.stdout);

var playerNumber

//takes user input of how many players to prompt names for player 1, player 2, etc.
function readPlayers(playerCount, playerArr, curr = 0) {
    if (playerCount < 5 || playerCount == curr) { //error is either found here
        rl.close();
        return;
    }

    rl.question("Enter name of player " + (curr + 1) + ": ", function (playerName) {
        playerArr.push(playerName)
        readPlayers(playerCount, playerArr, curr + 1);
    });
}

function numberAndNames() {

    rl.question("Enter number of players: ", function (answer) {

        readPlayers(parseInt(answer), players);
        var playerNumber = answer
    });

    rl.on('close', function () {
        if (playerNumber < 5) { //or error is found here
            console.log("Insuficient players. A minimum of 5 players is required.")
            numberAndNames();
        } else {
        console.log("The players are: " + players.toString());
        }
    });
}

numberAndNames();