Created
July 21, 2021 14:51
-
-
Save samandar-boymurodov/7f42cb9fae8126d59a621ab2b464e0af to your computer and use it in GitHub Desktop.
Node JS readline module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const questions = [ | |
"What is your favorite programming language?", | |
"Where do you work?", | |
"Do you like Node JS?" | |
] | |
const collectAnswers = (questions, done) => { | |
const readline = require("readline") | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}) | |
const answers = [] | |
const answered = (answer) => { | |
answers.push(answer) | |
if (answers.length === questions.length) { | |
done(answers) | |
} else rl.question(questions[answers.length] + " ", answered) | |
} | |
rl.question(questions[0] + " ", answered) | |
} | |
collectAnswers(questions, answers => { | |
console.log("\nThank you for answering these questions :)") | |
console.log(`Your answers --> ${answers.join(" ")}`) | |
process.exit() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment