Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the EventEmitter class from the 'events' module.
Node.js
const [1] = require('events');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or misspelled class names like 'Emitter' or 'eventEmitter'.
Trying to import the whole module without destructuring.
✗ Incorrect
You import the EventEmitter class exactly as EventEmitter from the 'events' module.
2fill in blank
mediumComplete the code to create a new instance of EventEmitter.
Node.js
const emitter = new [1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or incorrect class names.
Trying to call the module name instead of the class.
✗ Incorrect
You create a new EventEmitter instance by calling new EventEmitter().
3fill in blank
hardFix the error in the code to add a listener for the 'start' event.
Node.js
emitter.[1]('start', () => { console.log('Started'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'listen' or 'onStart'.
Confusing with 'addListener' which also works but is less common.
✗ Incorrect
The correct method to add a listener is on. It listens for the event named 'start'.
4fill in blank
hardFill both blanks to emit the 'data' event with a message.
Node.js
emitter.[1]('[2]', 'Hello World');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'on' instead of 'emit' to send events.
Using wrong event names like 'start' instead of 'data'.
✗ Incorrect
You use emit to send out the event named data with the message.
5fill in blank
hardFill all three blanks to create an EventEmitter, add a listener for 'end', and emit the 'end' event.
Node.js
const [1] = new EventEmitter(); [1].[2]('end', () => { console.log('Finished'); }); [1].[3]('end');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Mixing up 'on' and 'emit' methods.
Using 'listener' which is not a method.
✗ Incorrect
You create an instance named emitter, use on to listen for 'end', and emit to trigger it.