0

I want to loop x amount of times while doing a readline each time. In my code after reading in the first input, the loop never moves onto the second iteration. I assume it has something to do with the inline function I created but I'm stuck as to why. Any help?

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
rl.question("Ok how many players will there be (MAX: 8 players)\n", function(playerCount) {
  const parsedInput = parseInt(playerCount);
  if (parsedInput > 0 && parsedInput <= 8) {
    let listOfNames = [];
    for (let i = 0; i < parsedInput; i++) {
      rl.question(`Enter name #${i}\n`, function gatherNames(name) {
        listOfNames.push(name);
        console.log(`Added ${name} to list of players\n`);
      });
    }
  }
});
1
  • Please post valid code, you're missing the ) at the end of the rl.question( function call. Commented Jan 3, 2023 at 22:54

1 Answer 1

2

You are calling rl.question(...) parsedInput times, before you even enter the response from the first one. The library doesn't seem to support that and just silently fails.

You need to call the next rl.question(...) only after you received response from the current question.
For that you will need to call in inside the callback and can't use the for loop. You will need to rely on the size of the listOfNames array to know when to stop.

readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});
rl.question("Ok how many players will there be (MAX: 8 players)\n", function(playerCount) {
    const parsedInput = parseInt(playerCount);
    if (parsedInput > 0 && parsedInput <= 8) {
        let listOfNames = [];
        const add_name = function(name) {
            listOfNames.push(name);
            console.log(`Added ${name} to list of players\n`);
            // if the array doesn't have the desired number of names, ask again.
            if (listOfNames.length < parsedInput) {
                rl.question(`Enter name #${listOfNames.length}\n`, add_name);
            }
            // else print them out and end it
            else {
                console.log(listOfNames)
                rl.close();
            }
        }
        rl.question(`Enter name #0\n`, add_name);
    }
});

A better approach would be with async/await code i.e. promises.
It would make extending the functionally a lot simpler and straight forward.
The flow of the code is a lot closer to what you have tried.

const readlinePromises = require('readline/promises');

const rl = readlinePromises.createInterface({
    input: process.stdin,
    output: process.stdout,
});
(async () => {
    const playerCount = await rl.question("Ok how many players will there be (MAX: 8 players)\n");
    const parsedInput = parseInt(playerCount);
    if (parsedInput > 0 && parsedInput <= 8) {
        let listOfNames = [];
        for (let i=0; i<parsedInput; i++) {
            const name = await rl.question(`Enter name #${i}\n`);
            listOfNames.push(name);
            console.log(`Added ${name} to list of players\n`);
        }
        console.log(listOfNames);
    }
    rl.close();
})();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.