Skip to content

Commit 0b290d1

Browse files
add: fixing setting up a callback
1 parent 934c2b3 commit 0b290d1

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- [Filling all the missed angle brackets](filling-all-the-missed-angle-brackets)
44
- [Finding all the anagrams](finding-all-the-anagrams)
55
- [Finding the maximum depth of a binary tree](finding-the-maximum-depth-of-a-binary-tree)
6+
- [Fixing setting up a callback](fixing-setting-up-a-callback)
67
- [Getting the distinct transactions](getting-the-distinct-transactions)
78
- [Replacing with the cipher letters](replacing-with-the-cipher-letters)
89
- [Validating a maze path](validating-a-maze-path)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Fixing setting up a callback
2+
3+
The below code has an error. Fix it.
4+
5+
```javascript
6+
const solution = (messages) => {
7+
class Emitter {
8+
constructor(messages = []) {
9+
this.messages = messages;
10+
this.event = () => {};
11+
}
12+
13+
setEvent(fn) {
14+
this.event = fn;
15+
}
16+
17+
trigger() {
18+
this.messages.forEach((message) =>
19+
this.event(message),
20+
);
21+
}
22+
}
23+
24+
class Receiver {
25+
constructor() {
26+
this.messages = [];
27+
}
28+
29+
ping(message) {
30+
this.messages.push(message);
31+
}
32+
}
33+
34+
const myEmitter = new Emitter(messages);
35+
const myReceiver = new Receiver();
36+
37+
myEmitter.setEvent(myReceiver.ping);
38+
myEmitter.trigger();
39+
40+
return myReceiver.messages;
41+
};
42+
```
43+
44+
Input
45+
46+
```console
47+
[
48+
'Hello~',
49+
'Can you hear me?',
50+
'Nice to meet you.',
51+
'This is a test message.',
52+
]
53+
```
54+
55+
Output
56+
57+
```console
58+
[
59+
'Hello~',
60+
'Can you hear me?',
61+
'Nice to meet you.',
62+
'This is a test message.',
63+
]
64+
```
65+
66+
## Execute
67+
68+
```bash
69+
node solution.js
70+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const solution = (messages) => {
2+
class Emitter {
3+
constructor(messages = []) {
4+
this.messages = messages;
5+
this.event = () => {};
6+
}
7+
8+
setEvent(fn) {
9+
this.event = fn;
10+
}
11+
12+
trigger() {
13+
this.messages.forEach((message) =>
14+
this.event(message),
15+
);
16+
}
17+
}
18+
19+
class Receiver {
20+
constructor() {
21+
this.messages = [];
22+
}
23+
24+
ping(message) {
25+
this.messages.push(message);
26+
}
27+
}
28+
29+
const myEmitter = new Emitter(messages);
30+
const myReceiver = new Receiver();
31+
32+
myEmitter.setEvent(myReceiver.ping.bind(myReceiver));
33+
myEmitter.trigger();
34+
35+
return myReceiver.messages;
36+
};
37+
38+
(() => {
39+
console.log(
40+
solution([
41+
'Hello~',
42+
'Can you hear me?',
43+
'Nice to meet you.',
44+
'This is a test message.',
45+
]),
46+
);
47+
})();

0 commit comments

Comments
 (0)