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.
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());
});
It does not even begin prompting for the name of player 1. Why so? Help is appreciated thanks everyone