var n
var players = []
var rl = readline.createInterface(process.stdin, process.stdout);
rl.question("Enter number of players ", function(answer) {
//Use number of players to generate array of player names
for (n = 0; n < parseInt(answer); n++) {
var playerNumber = n + 1
rl.question("Enter name of player " + playerNumber, function(answer) {
players.push(answer)});
// stops prompting after asking for name of player 1?
}
});
rl.close;
rl.on('close', function(){
console.log("The players are: " + players.toString());
});
This works until “Enter name of player 1”, which the code stores the value of. Code does not prompt the rest of the players. Why?
I also tried replacing it with the ‘for’ loop with a ‘while’ loop, but it came out even worse. It does not even begin prompting for the name of player 1. Why so? Help is appreciated :) thanks everyone
var readline = require('readline');
var n
var players = []
var rl = readline.createInterface(process.stdin, process.stdout);
rl.question("Enter number of players ", function(answer) {
//Use number of players to generate array of player names
while (n < parseInt(answer)) {
var playerNumber = n + 1
rl.question("Enter name of player " + playerNumber, function(answer) {
players.push(answer)});
n++;
// does not even start prompting name of player 1?
}
});
rl.close;
rl.on('close', function(){
console.log("The players are: " + players.toString());
});
Edit: Tried moving rl.close out of the loops. The issue is still the same, this is what shows up in my console:
Enter number of players
(Input) 4
Enter name of player 1
(Input) Player 1
(Console stops prompting, when I tried adding more names and ending the process, the array only stores the name of player 1.)