Skip to content

Instantly share code, notes, and snippets.

@mnyamor
Last active October 27, 2016 22:21
Show Gist options
  • Save mnyamor/5b89adaba715856ecc0ac257ce897e28 to your computer and use it in GitHub Desktop.
Save mnyamor/5b89adaba715856ecc0ac257ce897e28 to your computer and use it in GitHub Desktop.
Node process
var questions = [
"What is your name?",
"what is your favourite hobby?",
"what is your favourite programming language?"
];
var answers = [];
function askQuestion(i) {
process.stdout.write(`\n\n\n ${questions[i]}`);
process.stdout.write(" > ");
}
process.stdin.on('data', function(data) {
answers.push(data.toString().trim());
if(answers.length < questions.length) {
askQuestion(answers.length);
} else {
process.exit();
}
});
/*Listen for the exit event on the process object.
When process exit is invoked, this callback function will fire
*/
process.on('exit', function(){
//display answers to the user
//add padding at the top
process.stdout.write('\n\n\n');
process.stdout.write(`Go ${answers[1]} ${answers[0]} you can finish writing ${answers[2]} later`);
//add padding at the bottom
process.stdout.write('\n\n\n');
});
askQuestion(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment