-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path2-method.js
More file actions
35 lines (29 loc) · 707 Bytes
/
Copy path2-method.js
File metadata and controls
35 lines (29 loc) · 707 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
'use strict';
// Task: rewrite `total` method from callbacks to async method
class Basket {
#items = null;
constructor(items) {
this.#items = items;
}
total(callback) {
let result = 0;
for (const item of this.#items) {
if (item.price < 0) {
callback(new Error('Negative price is not allowed'));
return;
}
result += item.price;
}
callback(null, result);
}
}
const electronics = [
{ name: 'Laptop', price: 1500 },
{ name: 'Keyboard', price: 100 },
{ name: 'HDMI cable', price: 10 },
];
const basket = new Basket(electronics);
basket.total((error, money) => {
if (error) console.error({ error });
else console.log({ money });
});