-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
38 lines (30 loc) · 744 Bytes
/
solution.js
File metadata and controls
38 lines (30 loc) · 744 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const solution = (messages) => {
class Emitter {
constructor(messages = []) {
this.messages = messages;
this.event = () => {};
}
setEvent(fn) {
this.event = fn;
}
trigger() {
this.messages.forEach((message) => this.event(message));
}
}
class Receiver {
constructor() {
this.messages = [];
}
ping(message) {
this.messages.push(message);
}
}
const myEmitter = new Emitter(messages);
const myReceiver = new Receiver();
myEmitter.setEvent(myReceiver.ping.bind(myReceiver));
myEmitter.trigger();
return myReceiver.messages;
};
(() => {
console.log(solution(['Hello~', 'Can you hear me?', 'Nice to meet you.', 'This is a test message.']));
})();