I find it very hard to explain or search for this problem, so I'm asking Stack Overflow.
I'm asking user for user-input in the terminal several times and want to make a function out of it. A function that takes a question and a variable, and the input should be added to the variable.
This is my code:
var name = 'hello',
age = '11'
var readline = require('readline');
// var rl = readline.createInterface(process.stdin, process.stdout);
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var getInput = function(inputVariable, question, cb) {
rl.question(question, function(answer) {
inputVariable = answer;
rl.close();
cb();
});
}
var askForName = function() {
console.log(1,age, name);
getInput(name, "What's your name? ", askForAge);
}
var askForAge = function() {
console.log(2,age, name);
getInput(age, "How old are you? ", printIt);
}
var printIt = function() {
console.log("Hello, " + name);
console.log(".. and " + age + " old.");
}
askForName();
Thanks!