I am constructing a Yahtzee in node.js. I use below code to ask user for input. The answer needs to be stored in a variable. I assume [answer] is used to temporarily store the answer value, but how can I get out the [answer] to an array without to many change of the code structure ?
Basic code structure:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Which dices to keep [1,2,3,4,5] ?: ", (answer) => {
console.log("Will keep dices: ", answer);
rl.close();
});
Extended basic code structure, to add the user input answer, into a variable:
var lines; // Added compared to basic code.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Which dices to keep [1,2,3,4,5] ?: ", (answer) => {
lines.push(answer); // Added compared to basic code.
console.log("Will keep dices: ", answer);
rl.close();
});
console.log(lines); // Added compared to basic code.
Result from terminal: Undefined.