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 () 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 () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
Why is the first of two entries for process.stdin.on, ‘data’ ? I cant find another ‘data’ anywhere in app.js or game.js. I checked on line and any examples I can find, they just use ‘data’ is well. is this merely convention or is it referencing another ‘data’ somewhere else in ‘process.’ ? What other entries can be used instead of ‘data’ and what do they do?
One thing that was helpful for me was to watch Crash Course computer Science - it explains the background that is being used in node.js and other backend applications of languages.
Why does the game keep looping if we get the wrong number? And how does Node know to accept another number and to only quit program when we type quit of get the number correct?
I’m expecting to see some kind of a loop or more code that tells node to do that explicitly, but I don’t see it. I would expect to see it in the last if statement of the games.js else function. What an I missing or not understanding?
Yeah they could have introduced event types at this point to avoid a lot of confusion for us… Took me a lot of digging to find, and it doesn’t seem that the node documentation has a reference for all event types, they are scattered around under each applicable module. ‘Data’ is a ‘stream’ module event of class ‘readable.’ Here’s the link:
So event names are limited to a specific set of keywords, then? Or do specific event names have special effects? Because the previous lesson created an event emitter with the name “celebration” so I figured you could name it whatever you wanted. But that was also attached to declaring an eventEmitter, not a process.stdin.
Hopefully this begins to make sense later on down the line; it seems like I’m missing fundamental information…
I could be wrong, I’m in the learning stage myself, and this is what I could figure out looking into it since it wasn’t really explained. I think the answer is both? For example, the doc for the events module lists two events for EventEmitter, ‘newListener’ and ‘removeListener.’ Those would be its built-in events. But the EventEmitter class allows you to create your own custom events with the emit() method, and handle them with on(). Here’s an example I took from tutorials teacher:
var emitter = require('events').EventEmitter;
function LoopProcessor(num) {
var e = new emitter();
setTimeout(function () {
for (var i = 1; i <= num; i++) {
e.emit('BeforeProcess', i);
console.log('Processing number:' + i);
e.emit('AfterProcess', i);
}
}
, 2000)
return e;
}
var lp = LoopProcessor(3);
lp.on('BeforeProcess', function (data) {
console.log('About to start the process for ' + data);
});
lp.on('AfterProcess', function (data) {
console.log('Completed processing ' + data);
});
Here they defined two events, ‘BeforeProcess’ and ‘AfterProcess’ with emit(), and defined how to handle them with on(). I think that node gives you the ability to create custom events as needed, but since many types of events will be common place, they have already defined those and provided event handler methods for you.
Like I said I could be wrong, and if anyone else knows better than I do, I’d love to hear it, but I’ve noticed the lesson FAQ’s don’t seem to get too much attention, unfortunately.
Thanks for looking into it! My suspicion is that, yes, you can provide whatever name you want to events when you create event emitters, but process.stdin has only a specific set of accepted names because it expects only a specific set of input types and that name serves to determine how it should behave.
process.stdin.on('data', callbackfunction) is basically an event listener for receiving ‘data,’ so that’s why you can’t just write whatever you want there. It’s like adding an onSpaghetti event listener in the browser; doesn’t mean anything and the browser isn’t equipped to respond to spaghetti.
Hi guys. Just read the documentation at node.js for process.stdin, which returns a Stream(net.Socket) which have the ‘data’ listener and ‘data’ would be a Buffer or String based on the documention, resulting in the need for toString function. Thats my best guess. Anyone with a better answer feel free to add on. Cheers!