-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercises.js
182 lines (147 loc) · 4.24 KB
/
exercises.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
if (42) {
if (42 == "42") {
if (true) {
if (42 == true) {
console.log("All that is true will succeed in the end.");
} else {
console.log("So close, but no.");
}
} else {
console.log("Inconceivable!");
}
} else {
console.log("But they look the same!!");
}
} else {
console.log("It is the meaning of life, but isn't truthy?!?!?");
}
function callSomeoneLater(name, callback) {
let messageEvent = {
message: `Hi ${name}!`
};
callback.bind(messageEvent);
}
// but why does this use of `callSomeoneLater` not work?
function MyObject(name) {
this.name = name;
this.invokeGreeting = () => {
callSomeoneLater(this.name, (message) => {
console.log(this);
console.log(message);
}) ;
}
}
let kelly = new MyObject("Kelly");
kelly.invokeGreeting();
function NamedList(name, items) {
let innerValues = items;
return Object.freeze({
name,
[Symbol.iterator]: function * () {
yield * innerValues;
}
})
}
let tens = new NamedList("byTens", [10, 20, 30]);
for (let num of tens) {
console.log(num);
}
const messageFactory = (errorCode = 500,
message = "Unknown server error",
severity = "FATAL error") => {
return Object.freeze({
report: () => `${severity} ${errorCode}: ${message}`
});
};
const warning = (factory) => {
return (errorCode, message) => factory(errorCode, message, "WARNING");
};
const warningFactory = warning(messageFactory);
console.log( messageFactory(401, "Unauthorized").report() );
console.log( warningFactory(455, "Disk low on space").report() );
const ERROR_INFORMATION = {
500: "Something bad happened, we are not sure what",
455: "Contact support, your disk cuota needs to be increased",
401: "Are you sure you are who you think you are?!?"
};
function extractError({code, text, level}) {
return { errorCode: code, message: text, warning: level };
}
function informError(errorObject) {
let {errorCode} = errorObject;
let info = ERROR_INFORMATION[errorCode];
return { ...errorObject, info };
}
// --- test it out:
let testError = {
code: 500,
text: "Unknown server error",
server: "www.xyz.com",
time: 1583793829,
level: "FATAL"
};
let result = informError(extractError(testError));
console.log(result);
class Counter {
#count = 0;
constructor(initialValue) {
this.#count = initialValue;
}
increment(delta) {
this.count += delta;
}
report() {
return this.count;
}
}
// test code:
let counter = new Counter(10);
counter.increment(5);
let result1 = counter.report();
console.log(counter.count);
async function fetchEventFromServer(eventId) {
// mocked response
return Promise.resolve({
id: eventId,
internalId: `int-${eventId.slice(2)}`,
message: `something for event ${eventId}`
});
}
async function * dedupedEvents(...fetchEventIds) {
let seenInternalIds = new Set();
let eventIdToInternalIdMap = {};
for (let eventId of fetchEventIds) {
if (!seenInternalIds.has(eventIdToInternalIdMap[eventId])) {
let eventBody = await fetchEventFromServer(eventId);
eventIdToInternalIdMap[eventId] = eventBody.internalId;
if (!seenInternalIds.has(eventBody.internalId)) {
seenInternalIds.add(eventBody.internalId);
yield eventBody;
}
}
}
}
// test the code...
(async () => {
let eventList = ['e-1', 'e-2', 'x-1', 'e-3', 'e-4', 'x-2', 'e-5', 'z-5', 'e-1'];
for await (let event of dedupedEvents(...eventList)) {
console.log(event.internalId);
}
})();
function callSomeoneLater(name, callback) {
let messageEvent = {
message: `Hi ${name}!`
};
callback.apply(messageEvent);
}
// but why does this use of `callSomeoneLater` not work?
function MyObject(name) {
this.name = name;
this.invokeGreeting = () => {
callSomeoneLater(this.name, () => {
console.log(this.message);
}) ;
}
}
let kelly = new MyObject("Kelly");
kelly.invokeGreeting();