I am creating a program to take input of two numbers from the command line and then showing there sum in node.js. I am using readline module to take stdin. Below is my code.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const r2 = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Please enter the first number', (answer1) => {
r2.question('Please enter the second number', (answer2) => {
var result = (+answer1) + (+answer2);
console.log(`The sum of above two numbers is ${result}`);
});
rl.close();
});
This program just show me "Please enter the first number" and when i enter a number like 5, it takes 5 for second input also and shows the answer 10
It don't ask second question at all. Please check this and tell me what is the problem. And if there is any better way to take multiple input please tell that.
I am a novice user in node.js
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…