Skip to content

Commit 8ec34f6

Browse files
committed
Add examples
1 parent 8138733 commit 8ec34f6

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

JavaScript/1-inconsistent.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const inconsistent = (a, b) => {
4+
if (a < 0) return false;
5+
if (b > 9) return b;
6+
if (a === b) return -1;
7+
const result = a + b;
8+
if (typeof result === 'number') return result;
9+
};
10+
11+
const y1 = inconsistent(-1, 7);
12+
const y2 = inconsistent(5, 20);
13+
const y3 = inconsistent(5, 5);
14+
const y4 = inconsistent(5, 7);
15+
const y5 = inconsistent(5, '7');
16+
console.log({ y1, y2, y3, y4, y5 });

JavaScript/2-consistent.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const inconsistent = (a, b) => {
4+
if (a < 0) throw new Error('Expected positive "a"');
5+
if (b > 9) return b;
6+
if (a === b) return -1;
7+
const result = a + b;
8+
if (typeof result === 'number') return result;
9+
throw new Error('Result is not number');
10+
};
11+
12+
try {
13+
const y1 = inconsistent(-1, 7);
14+
console.log({ y1 });
15+
} catch (error) {
16+
console.error(error.message);
17+
}
18+
19+
try {
20+
const y2 = inconsistent(5, 20);
21+
console.log({ y2 });
22+
} catch (error) {
23+
console.error(error.message);
24+
}
25+
26+
try {
27+
const y3 = inconsistent(5, 5);
28+
console.log({ y3 });
29+
} catch (error) {
30+
console.error(error.message);
31+
}
32+
33+
try {
34+
const y4 = inconsistent(5, 7);
35+
console.log({ y4 });
36+
} catch (error) {
37+
console.error(error.message);
38+
}
39+
40+
try {
41+
const y5 = inconsistent(5, '7');
42+
console.log({ y5 });
43+
} catch (error) {
44+
console.error(error.message);
45+
}

JavaScript/3-initial.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const f = ({ a, b }, callback) => {
4+
if (a < 0) return callback(0);
5+
if (b > 9) return callback(100);
6+
if (a === b) return callback(-1);
7+
callback(b - a);
8+
};
9+
10+
f({ a: 5, b: 7 }, (res) => {
11+
console.log(res);
12+
if (res === 1) return res;
13+
});

JavaScript/4-return.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const f = ({ a, b }, callback) => {
4+
if (a < 0) {
5+
callback(0);
6+
return;
7+
}
8+
if (b > 9) {
9+
callback(100);
10+
return;
11+
}
12+
if (a === b) {
13+
callback(-1);
14+
return;
15+
}
16+
callback(b - a);
17+
};
18+
19+
f({ a: 5, b: 7 }, (res) => {
20+
console.log(res);
21+
if (res === 1) return res;
22+
});

JavaScript/5-void.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const f = ({ a, b }, callback) => {
4+
if (a < 0) return void callback(0);
5+
if (b > 9) return void callback(100);
6+
if (a === b) return void callback(-1);
7+
void callback(b - a);
8+
};
9+
10+
f({ a: 5, b: 7 }, (res) => {
11+
console.log(res);
12+
if (res === 1) return res;
13+
});

0 commit comments

Comments
 (0)