Skip to content

Commit 600e90d

Browse files
committed
Add tasks
1 parent 52c7999 commit 600e90d

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

JavaScript/Tasks/1-adapter.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
// Task: create Promise-returning adapter function `totalAsync`
4+
// Do not change function `total` contract, just call `total` from
5+
// `totalAsync` and convert contracts
6+
7+
// Should not be changed
8+
const total = (items, callback) => {
9+
let result = 0;
10+
for (const item of items) {
11+
if (item.price < 0) {
12+
callback(new Error('Negative price is not allowed'));
13+
return;
14+
}
15+
result += item.price;
16+
}
17+
callback(null, result);
18+
};
19+
20+
// const totalAsync = (items) => new Promise...
21+
22+
const electronics = [
23+
{ name: 'Laptop', price: 1500 },
24+
{ name: 'Keyboard', price: 100 },
25+
{ name: 'HDMI cable', price: 10 },
26+
];
27+
28+
// Also rewrite call, use .then instead of callback
29+
total(electronics, (error, money) => {
30+
if (error) console.error({ error });
31+
else console.log({ money });
32+
});

JavaScript/Tasks/2-promise.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
// Task: change `iterate` contract from chainable callbacks
4+
// to Promise (chainable or you can call it with await syntax)
5+
6+
const iterate = (items) => {
7+
let index = 0;
8+
const chain = {
9+
next: (callback) => {
10+
if (index < items.length) {
11+
callback(items[index++]);
12+
}
13+
return chain;
14+
}
15+
};
16+
return chain;
17+
};
18+
19+
const electronics = [
20+
{ name: 'Laptop', price: 1500 },
21+
{ name: 'Keyboard', price: 100 },
22+
{ name: 'HDMI cable', price: 10 },
23+
];
24+
25+
// Use await syntax to get items
26+
iterate(electronics).next((item) => {
27+
console.log(item);
28+
}).next((item) => {
29+
console.log(item);
30+
}).next((item) => {
31+
console.log(item);
32+
});

JavaScript/Tasks/3-reject.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
// Task: support rejection with an error, if no more items in
4+
// `items` array are available to return with `.next()`
5+
// Change throwing error to returning rejected Promise.
6+
7+
const iterate = (items) => {
8+
let index = 0;
9+
return {
10+
next: () => new Promise((fulfill) => {
11+
if (index < items.length) {
12+
return fulfill(items[index++]);
13+
}
14+
throw new Error('No more items to iterate');
15+
})
16+
};
17+
};
18+
19+
const electronics = [
20+
{ name: 'Laptop', price: 1500 },
21+
{ name: 'Keyboard', price: 100 },
22+
{ name: 'HDMI cable', price: 10 },
23+
];
24+
25+
(async () => {
26+
const items = iterate(electronics);
27+
const item1 = await items.next();
28+
console.log(item1);
29+
const item2 = await items.next();
30+
console.log(item2);
31+
const item3 = await items.next();
32+
console.log(item3);
33+
const item4 = await items.next();
34+
console.log(item4);
35+
})();

JavaScript/Tasks/4-chain.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
// Task: here is given code you can't touch but you need to
4+
// write solution below to sum given prices (wrapped in promises).
5+
// Use Promise.all to resolve promises.
6+
// Use functions: `formatMoney` and `sum` as utilities.
7+
// Additional task: add .catch and .finally blocks in chain
8+
9+
// Do not touch following code
10+
11+
const CURRENCY_RATE = 1.09;
12+
13+
const convert = ({ price }) => Promise.resolve(price * CURRENCY_RATE);
14+
15+
const electronics = [
16+
{ name: 'Laptop', price: 1500 },
17+
{ name: 'Keyboard', price: 100 },
18+
{ name: 'HDMI cable', price: 10 },
19+
];
20+
21+
const prices = electronics.map(convert);
22+
23+
const formatMoney = (x) => parseFloat(x.toFixed(2));
24+
25+
const sum = (a, b) => a + b;
26+
27+
// Write solution below

0 commit comments

Comments
 (0)