FAQ: Learn Node.js - User Input/Output

This community-built FAQ covers the “User Input/Output” exercise from the lesson “Learn Node.js”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Node.js

FAQs on the exercise User Input/Output

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Where is the logic that keeps repeating the prompt for user input? It’s not clear to me how/why the program continues to loop until it reaches exit().

Node.js doc suggests that sometimes we should not call process.exit() explicitly, which may make the data print to stdout lost. So how to make the game.js better?

There is no loop. Since we declare an event listener, Node.js will wait for the emitting of the data event. Right after the event is fired, Node.js invoke the callback function and then return to the waiting state, waiting for the next event.

3 Likes

Can anyone explain process.exit( ) in depth ?

The userInput we receive is an instance of the Node Buffer class.

can anyone explain what does this mean ?

1 Like

In game.js the following line of code is used to generate a random number 1 - 10.

let secretValue = Math.floor(1 + Math.random() * 10).toString();

Wouldn’t it be in fact neater to use Math.ceil() instead of 1 + Math.floor() to get a range that excludes 0?

let secretValue = Math.ceil(Math.random() * 10).toString();

https://www.codecademy.com/paths/back-end-engineer-career-path/tracks/becp-build-a-back-end-with-node-express-js/modules/fscp-learn-node-js/lessons/node/exercises/user-io

let {testNumber} = require(’./game.js’);

Why is testNumber inside { }?

Because from the game module we are exporting an object with one property (testNumber) and here
In app.js we are destructuring that property from the object.

1 Like

In computer science, a data buffer (or just buffer ) is a region of a physical memory storage used to temporarily store data while it is being moved from one place to another. … However, a buffer may be used when moving data between processes within a computer . This is comparable to buffers in telecommunication.
Buffer in Node is a class and when you const myBuffer = new Buffer() do this(the new keyword uses the constructor of that class to create an instance of the class, so in this case myBuffer is an instance of the Buffer class.

1 Like

Thank you for your response. Destructuring was what I was looking for. Codecademy didn’t not teach that prior to it’s use in another example of pushing the teaching responsibility onto the student.

1 Like

Destructuring was taught around Redux chapter, hopes to be useful

I just took a look, I could have missed it, but I didn’t see a Redux chapter - which track are you using?

I’m using the Backend Engineer course

To clarify this question for everyone:

What is a Buffer ?

→ A buffer simply means ones and zeros. That’s why we would need to convert the data we get back to string before printing.

Does the first argument for process.stdin.on() always have to be ‘data’ or can it be any string we have created?

process.stdin.on('data', playGame);
process.stdin.on('random', playGame);

When I change ‘data’ to ‘random’ I get the below error in the terminal?

node app.js
I’m thinking of a number from 1 through 10. What do you think it is?
(Write “quit” to give up.)

Is the number … $ 5

bash: 5: command not found

I thought its similar to an instance of the EventEmitter where in the on() method we can pass anything as an string as an event to be listened out for. Or is that a incorrect assumption?

for example

// Assign the newUserListener function as the listener callback for 'new user' events
myEmitter.on('new user', newUserListener)
 
// Emit a 'new user' event
myEmitter.emit('new user', 'Lily Pad') 

I assume 'new user could be changed for another string of your choice and the code will still work. So why does it not work for process.stdin.on(‘data’, playGame); ?

1 Like

I had the same question just now. I did some digging in the documentation of node and in stackoverflow, and what I found is that (to my understanding) stdin and stdout is a stream (in prior lessons in node there were explanations about writable, readable and duplex streams types). In our case, stdin is a readable stream, and readable stream has built in events such as “data” and some others. Here is the link to the documentation, it should help. Stream | Node.js v17.5.0 Documentation

3 Likes