Skip to content

Commit 466cb41

Browse files
committed
Add more examples
1 parent e32af67 commit 466cb41

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

JavaScript/Tasks/3-constructor.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
// Task: rewrite class Basket to class Total with async constructor
4+
// Constructor call example:
5+
// const total = await new Total(electronics);
6+
// console.log({ total });
7+
8+
class Basket {
9+
#items = null;
10+
11+
constructor(items) {
12+
this.#items = items;
13+
}
14+
15+
total(callback) {
16+
let result = 0;
17+
for (const item of this.#items) {
18+
if (item.price < 0) {
19+
callback(new Error('Negative price is not allowed'));
20+
return;
21+
}
22+
result += item.price;
23+
}
24+
callback(null, result);
25+
}
26+
}
27+
28+
const electronics = [
29+
{ name: 'Laptop', price: 1500 },
30+
{ name: 'Keyboard', price: 100 },
31+
{ name: 'HDMI cable', price: 10 },
32+
];
33+
34+
const basket = new Basket(electronics);
35+
basket.total((error, money) => {
36+
if (error) console.error({ error });
37+
else console.log({ money });
38+
});

JavaScript/Tasks/4-default.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
// Task: optimize `total` call with default value 0 (as in example)
4+
// to have one-line solution - 1 line instead of 5 lines
5+
// (call and default value in one line)
6+
7+
// Do not change code before usage block
8+
9+
const total = async (items) => {
10+
let result = 0;
11+
for (const item of items) {
12+
if (item.price < 0) {
13+
throw new Error('Negative price is not allowed');
14+
}
15+
result += item.price;
16+
}
17+
return result;
18+
};
19+
20+
const electronics = [
21+
{ name: 'Laptop', price: -1500 },
22+
{ name: 'Keyboard', price: 100 },
23+
{ name: 'HDMI cable', price: 10 },
24+
];
25+
26+
// Usage block: change just following code
27+
28+
(async () => {
29+
let money = 0;
30+
try {
31+
money = await total(electronics);
32+
} catch {
33+
}
34+
console.log({ money });
35+
})();

JavaScript/Tasks/5-exists.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
// Task: optimize `fileExists` function to one-liner
4+
// Do not change "Usage" section, edit just "Implementation"
5+
6+
const fs = require('node:fs');
7+
8+
// Implementation
9+
10+
const fileExists = async (name) => {
11+
try {
12+
await fs.promises.access(name);
13+
return true;
14+
} catch {
15+
return false;
16+
}
17+
};
18+
19+
// Usage
20+
21+
(async () => {
22+
const name = 'file-name.ext';
23+
const exists = await fileExists(name);
24+
console.log({ name, exists });
25+
})();
26+
27+
(async () => {
28+
const name = '5-exists-problem.js';
29+
const exists = await fileExists(name);
30+
console.log({ name, exists });
31+
})();

0 commit comments

Comments
 (0)