Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a listener that runs only once.
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 'on' instead of 'once' causes the listener to run multiple times.
Using 'emit' instead of a listener method.
✗ Incorrect
The 'once' method adds a listener that is called only the first time the event is emitted.
2fill in blank
mediumComplete the code to emit the 'ready' event once.
Node.js
emitter.[1]('ready');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'once' to emit events instead of listen.
Using 'on' which only adds listeners.
✗ Incorrect
The 'emit' method triggers the event and calls listeners.
3fill in blank
hardFix the error in adding a one-time listener for 'data' event.
Node.js
emitter.[1]('data', () => { console.log('Data received'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'on' causes the listener to run multiple times.
Using 'emit' instead of adding a listener.
✗ Incorrect
Use 'once' to add a listener that runs only once for the 'data' event.
4fill in blank
hardFill both blanks to add a one-time listener and then emit the event.
Node.js
emitter.[1]('finish', () => { console.log('Finished'); }); emitter.[2]('finish');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'on' instead of 'once' for the listener.
Using 'on' instead of 'emit' to trigger the event.
✗ Incorrect
Use 'once' to add a one-time listener and 'emit' to trigger the event.
5fill in blank
hardFill all three blanks to add a one-time listener, emit the event, and verify it runs only once.
Node.js
let count = 0; emitter.[1]('ping', () => { count++; }); emitter.[2]('ping'); emitter.[3]('ping'); console.log(count);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'on' instead of 'once' causes count to increase twice.
Using 'once' but not emitting the event.
✗ Incorrect
Use 'once' to add a one-time listener, then 'emit' twice to show the listener runs only once, so count is 1.