-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path4-default.js
More file actions
38 lines (31 loc) · 794 Bytes
/
4-default.js
File metadata and controls
38 lines (31 loc) · 794 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
36
37
38
'use strict';
// Task: optimize `total` call with default value 0 (as in example)
// to have one-line solution - 1 line instead of 5 lines
// (call and default value in one line)
// Do not change code before usage block
const total = async (items) => {
let result = 0;
for (const item of items) {
if (item.price < 0) {
throw new Error('Negative price is not allowed');
}
result += item.price;
}
return result;
};
const electronics = [
{ name: 'Laptop', price: -1500 },
{ name: 'Keyboard', price: 100 },
{ name: 'HDMI cable', price: 10 },
];
// Usage block: change just following code
const main = async () => {
let money = 0;
try {
money = await total(electronics);
} catch {
console.log('catched');
}
console.log({ money });
};
main();