- [Instructor] So with our file,
we want to change the code a bit to collect answers
from our terminal.
So the first thing we want to do
is let's make sure to move our ask function
above our process standard input.
Another thing I'm noticing here
is that this is looking for answers.length.
If we wanted to set this to be an empty array,
we could always add a default here.
So we could add a default argument to this function
and remove this from our function invocation here.
So that's one thing we can do.
The second thing I want to do
is instead of echoing the answer back out,
let's go ahead and add this to the array.
So we're going to replace process standard input.write
with answers.push.
We'll take data, convert it to a string
and trim off any white space.
The next thing we want to do is say if our answers.length
is less than our questions.length,
then we want to call the ask function again
with answers.length.
So whatever the length of that answers array is.
Otherwise, if there are as many answers
as there are questions,
we want to call process.exit
to tell our process to quit.
Now if we run node global again,
it'll say what is your name?
What would you rather be doing?
What is your preferred programming language?
And there we go.
Now we can answer our questions
and as we cycle through them,
we're saving our answers
but we've collected them
but we're not really doing anything with them yet.
So let's go back and display the results.
In addition to this data event
where if we have some new data,
we're going to do everything that's inside of this function.
We're going to call process.on
and whenever we call exit,
we're going to call another function.
So check this out.
First, we're going to say process.standard output.
So write this to the terminal.
We'll add some spaces here.
Then we'll say process standard output.write
and we're going to use a template string
to add the message.
We'll say Go answers one.
We'll add a space.
Answers zero
and then you can say you can finish
writing answers two later.
Okay, so that's a long, long string
but check this out.
This is going to pluck the second item from the array,
the first item from the array, our name,
and then the programming language of choice
to add that to our string.
So let's see what we get.
We're going to take another look.
Node global, what's your name?
What would you rather be doing?
What's your preferred programming language.
Go Skiing Eve you can finish writing JavaScript later.
So our program is extremely friendly.
It's some good advice.
I'm going to trust the technology
and end the lesson here.
But before I do, remember the standard input
and standard output objects give us some powerful tools
for communication with running a process.
So these tools are available on the process object,
meaning that you can use them anywhere.