Turning Learners Into Developers
Codekilla
CODEKILLA
Hard
JS-005javascript

Build an EventEmitter

Problem

Implement a Node-style EventEmitter with `on`, `off`, `emit`, and `once`. Foundation of every pub/sub system.

Input
N/A — script demonstration.
Output
Lines printed by the listeners.
Constraints
up to 10^4 listeners
Sample input
e.on('tick', n => console.log('A' + n));
e.emit('tick', 1);
e.once('tick', n => console.log('B' + n));
e.emit('tick', 2);
e.emit('tick', 3);
Sample output
A1
A2
B2
A3
Explanation
once fires on `2` only; regular on fires on every emit.
designoopclosuresobserver@Netflix@Meta@Microsoft
Visible test cases
in: e.on('x', () => console.log('hi')); e.emit('x')
out: hi
Your solution — run it, use AI if stuck
javascript