-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy path5-errors.js
More file actions
67 lines (61 loc) · 1.76 KB
/
5-errors.js
File metadata and controls
67 lines (61 loc) · 1.76 KB
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
'use strict';
// Task: rewrite error handling to use callback-last-error-first
// contract to return errors instead of throwing them.
// So remove all try/catch blocks and pass errors to callbacks.
// Hint: You may also use error.cause to wrap escalated errors.
// Extra credit task: use AggregateError to combine escalated errors.
// Extra credit task: fix eslint error: "Function declared in a loop
// contains unsafe references to variable(s) 'total' no-loop-func"
const MAX_PURCHASE = 2000;
const calculateSubtotal = (goods, callback) => {
let amount = 0;
for (const item of goods) {
if (typeof item.name !== 'string') {
throw new Error('Noname in item in the bill');
}
if (typeof item.price !== 'number') {
throw new Error(`${item.name} price expected to be number`);
}
if (item.price < 0) {
throw new Error(`Negative price for ${item.name}`);
}
amount += item.price;
}
callback(amount);
};
const calculateTotal = (order, callback) => {
const expenses = new Map();
let total = 0;
const calc = (groupName) => (amount) => {
total += amount;
expenses.set(groupName, amount);
};
for (const groupName in order) {
const goods = order[groupName];
calculateSubtotal(goods, calc(groupName));
if (total > MAX_PURCHASE) {
throw new Error('Total is above the limit');
}
}
return callback({ total, expenses });
};
const purchase = {
Electronics: [
{ name: 'Laptop', price: 1500 },
{ name: 'Keyboard', price: 100 },
{ name: 'HDMI cable' },
],
Textile: [
{ name: 'Bag', price: 50 },
{ price: 20 },
],
};
try {
console.log(purchase);
calculateTotal(purchase, (bill) => {
console.log(bill);
});
} catch (error) {
console.log('Error detected');
console.error(error);
}