Skip to content

Commit 97443b0

Browse files
committed
Add tasks
1 parent 344559f commit 97443b0

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

JavaScript/Tasks/1-sync.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
// Task: rewrite function to return result into sync callback
4+
5+
// Change signature to: (items, callback(result))
6+
const total = (items) => {
7+
let result = 0;
8+
for (const item of items) {
9+
result += item.price;
10+
}
11+
return result;
12+
};
13+
14+
const electronics = [
15+
{ name: 'Laptop', price: 1500 },
16+
{ name: 'Keyboard', price: 100 },
17+
{ name: 'HDMI cable', price: 10 },
18+
];
19+
20+
// Use new signature total(electronics, (money) => ...)
21+
const money = total(electronics);
22+
console.log({ money });

JavaScript/Tasks/2-error.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
// Task: return an error for items with negative price
4+
// Hint: use callback-last-error-first contract
5+
6+
const total = (items, callback) => {
7+
let result = 0;
8+
for (const item of items) {
9+
result += item.price;
10+
}
11+
callback(result);
12+
};
13+
14+
const electronics = [
15+
{ name: 'Laptop', price: -1500 },
16+
{ name: 'Keyboard', price: 100 },
17+
{ name: 'HDMI cable', price: 10 },
18+
];
19+
20+
total(electronics, (money) => {
21+
console.log({ money });
22+
});

JavaScript/Tasks/3-async.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
// Task: rewrite `total` function to be async with JavaScript timers
4+
// Use `setInterval` and `clearInterval` to check next item each 1 second
5+
// Calculations will be executed asynchronously because of timers
6+
// Run `total` twice (as in example below) but in parallel
7+
// Print debug output for each calculation step (each second)
8+
//
9+
// Hint: example output:
10+
// { check: { item: { name: 'Laptop', price: 1500 } } }
11+
// { check: { item: { name: 'Laptop', price: 1500 } } }
12+
// { check: { item: { name: 'Keyboard', price: 100 } } }
13+
// { check: { item: { name: 'Keyboard', price: 100 } } }
14+
// { check: { item: { name: 'HDMI cable', price: 10 } } }
15+
// { check: { item: { name: 'HDMI cable', price: 10 } } }
16+
// { money: 1610 }
17+
// { money: 1610 }
18+
19+
const total = (items, callback) => {
20+
let result = 0;
21+
for (const item of items) {
22+
console.log({ check: { item } });
23+
if (item.price < 0) {
24+
callback(new Error('Negative price is not allowed'));
25+
return;
26+
}
27+
result += item.price;
28+
}
29+
callback(null, result);
30+
};
31+
32+
const electronics = [
33+
{ name: 'Laptop', price: 1500 },
34+
{ name: 'Keyboard', price: 100 },
35+
{ name: 'HDMI cable', price: 10 },
36+
];
37+
38+
total(electronics, (error, money) => {
39+
if (error) console.error({ error });
40+
else console.log({ money });
41+
});
42+
43+
total(electronics, (error, money) => {
44+
if (error) console.error({ error });
45+
else console.log({ money });
46+
});

JavaScript/Tasks/4-hell.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
// Task: refactor callback hell code with named callbacks
4+
// Restriction: you can change code only in "Usage" section
5+
6+
const getPurchase = (callback) => callback({
7+
Electronics: [
8+
{ name: 'Laptop', price: 1500 },
9+
{ name: 'Keyboard', price: 100 },
10+
{ name: 'HDMI cable', price: 10 },
11+
],
12+
Textile: [
13+
{ name: 'Bag', price: 50 },
14+
],
15+
});
16+
17+
const iterateGroups = (order, callback) => {
18+
for (const groupName in order) {
19+
const group = order[groupName];
20+
callback(group);
21+
}
22+
};
23+
24+
const groupTotal = (items, callback) => {
25+
let total = 0;
26+
for (const item of items) {
27+
total += item.price;
28+
}
29+
callback(total);
30+
};
31+
32+
const budget = (limit) => {
33+
let balance = limit;
34+
35+
const withdraw = (value, callback) => {
36+
const success = balance >= value;
37+
if (success) balance -= value;
38+
callback(success);
39+
};
40+
41+
const rest = (callback) => callback(balance);
42+
43+
return { withdraw, rest };
44+
};
45+
46+
// Usage
47+
48+
const wallet = budget(1650);
49+
50+
getPurchase((purchase) => {
51+
let amount = 0;
52+
iterateGroups(purchase, (group) => {
53+
groupTotal(group, (subtotal) => {
54+
wallet.withdraw(subtotal, (success) => {
55+
if (success) amount += subtotal;
56+
wallet.rest((balance) => {
57+
console.log({ success, amount, subtotal, balance });
58+
});
59+
});
60+
});
61+
});
62+
});

0 commit comments

Comments
 (0)