-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy path5-errors.js
More file actions
76 lines (65 loc) · 1.92 KB
/
5-errors.js
File metadata and controls
76 lines (65 loc) · 1.92 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
68
69
70
71
72
73
74
75
'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') {
return callback(new Error('Noname in item in the bill'))
}
if (typeof item.price !== 'number') {
return callback(new Error(`${item.name} price expected to be number`));
}
if (item.price < 0) {
return callback(new Error(`Negative price for ${item.name}`));
}
amount += item.price;
}
callback(null, amount);
};
const calculateTotal = (order, callback) => {
const expenses = new Map();
let total = 0;
const calc = (groupName) => (err, amount) => {
if(err){
return callback(err);
} else {
total += amount;
expenses.set(groupName, amount);
}
};
for (const groupName in order) {
const goods = order[groupName];
calculateSubtotal(goods, calc(groupName));
if (total > MAX_PURCHASE) {
return callback(new Error('Total is above the limit'));
}
}
return callback(null, { total, expenses });
};
const purchase = {
Electronics: [
{ name: 'Laptop', price: 1500 },
{ name: 'Keyboard', price: 100 },
{ name: 'HDMI cable' },
],
Textile: [
{ name: 'Bag', price: 50 },
{ price: 20 },
],
};
console.log(purchase);
calculateTotal(purchase, (err, bill) => {
if(err){
console.log('Error detected');
console.error(err);
} else {
console.log(bill);
}
});