Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the EventEmitter class from the 'events' module.
Express
const [1] = require('events');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like 'Emitter' or 'Event'.
Forgetting to import from 'events' module.
✗ Incorrect
The EventEmitter class is imported from the 'events' module to create event-driven behavior.
2fill in blank
mediumComplete the code to create a new instance of EventEmitter.
Express
const emitter = new [1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like 'Emitter' or 'Event'.
Forgetting the parentheses to create an instance.
✗ Incorrect
You create a new event emitter by calling new EventEmitter().
3fill in blank
hardFix the error in the code to listen for the 'start' event.
Express
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 event registration with event emitting.
✗ Incorrect
The correct method to listen for events is on. It registers a callback for the event.
4fill in blank
hardFill both blanks to emit a 'data' event with a message.
Express
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 trigger events.
Using wrong event names.
✗ Incorrect
Use emit to trigger an event. The event name here is data.
5fill in blank
hardFill all three blanks to create an event listener that logs the received message.
Express
emitter.[1]('message', ([2]) => { console.log([3]); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'emit' instead of 'on' to listen.
Using different variable names inconsistently.
✗ Incorrect
Use on to listen for 'message' events. The callback receives the message as msg and logs it.