Challenge - 5 Problems
Real-Time Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Node.js real-time event code?
Consider this Node.js code using EventEmitter. What will be printed to the console?
Node.js
const EventEmitter = require('events'); const emitter = new EventEmitter(); emitter.on('data', (msg) => { console.log('Received:', msg); }); emitter.emit('data', 'Hello'); emitter.emit('data', 'World');
Attempts:
2 left
💡 Hint
Think about how many times the 'data' event is emitted and how many listeners are attached.
✗ Incorrect
The 'data' event is emitted twice with different messages. The listener logs each message, so both lines print.
❓ state_output
intermediate1:30remaining
What is the final value of count after this real-time message simulation?
This code simulates receiving messages and counting them. What is the final count value?
Node.js
let count = 0; const EventEmitter = require('events'); const emitter = new EventEmitter(); emitter.on('message', () => { count += 1; }); emitter.emit('message'); emitter.emit('message'); emitter.emit('message');
Attempts:
2 left
💡 Hint
Each 'message' event increases count by one.
✗ Incorrect
The listener increments count on each 'message' event. Since it emits three times, count becomes 3.
📝 Syntax
advanced2:00remaining
Which option causes a syntax error in this real-time Node.js code?
Identify the option that will cause a syntax error when running this code snippet.
Node.js
const EventEmitter = require('events'); const emitter = new EventEmitter(); emitter.on('update', (data) => { console.log('Update:', data); }); emitter.emit('update', {value: 42});
Attempts:
2 left
💡 Hint
Look carefully at the parentheses and commas in the function call.
✗ Incorrect
Option D is missing a comma between the event name and the callback function, causing a syntax error.
🔧 Debug
advanced2:30remaining
Why does this real-time event listener never log messages?
This code intends to log messages received in real-time but logs nothing. Why?
Node.js
const EventEmitter = require('events'); const emitter = new EventEmitter(); emitter.emit('message', 'Hello'); emitter.on('message', (msg) => { console.log('Message:', msg); });
Attempts:
2 left
💡 Hint
Think about the order of event emission and listener registration.
✗ Incorrect
Events emitted before listeners are added are not caught. The listener must be registered before emitting.
🧠 Conceptual
expert3:00remaining
Why is real-time communication critical in a chat application?
Choose the best reason why real-time communication is essential for chat apps.
Attempts:
2 left
💡 Hint
Think about how people expect to chat in real life.
✗ Incorrect
Real-time means messages appear immediately, making conversations feel live and natural.