Skip to content

Commit fde10ba

Browse files
committed
Add more exercises with solutions
1 parent 278011d commit fde10ba

12 files changed

Lines changed: 128 additions & 0 deletions

File tree

Exercises/1-random.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
const random = (min, max) => {
4+
// Generate random Number between from min to max
5+
// Use Math.random() and Math.floor()
6+
// See documentation at MDN
7+
};
8+
9+
module.exports = { random };

Exercises/1-random.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
({
2+
name: 'random',
3+
length: [120, 150],
4+
test: random => {
5+
const x = random(0, 10);
6+
if (typeof x !== 'number') throw new Error('Expected number result');
7+
if (x < 0 || x > 10) {
8+
throw new Error('Expected random number from 0 to 10 inclusive');
9+
}
10+
11+
const y = random(0, 0);
12+
if (y !== 0) throw new Error('Expected 0');
13+
14+
const z = random(10, 10);
15+
if (z !== 10) throw new Error('Expected 10');
16+
}
17+
})
File renamed without changes.

Exercises/3-ip.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
const ipToInt = (ip = '127.0.0.1') => {
4+
// Parse ip address as string, for example '10.0.0.1'
5+
// to ['10', '0', '0', '1'] to [10, 0, 0, 1]
6+
// and convert to Number value 167772161 with sitwise shift
7+
// (10 << 8 << 8 << 8) + (0 << 8 << 8) + (0 << 8) + 1 === 167772161
8+
// Use Array.prototype.reduce of for loop
9+
};
10+
11+
module.exports = { ipToInt };

Exercises/3-ip.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
({
2+
name: 'ipToInt',
3+
length: [60, 150],
4+
cases: [
5+
['127.0.0.1', 2130706433],
6+
['10.0.0.1', 167772161],
7+
['192.168.1.10', -1062731510],
8+
['165.225.133.150', -1511946858],
9+
['0.0.0.0', 0],
10+
['8.8.8.8', 0x08080808],
11+
]
12+
})

Exercises/4-methods.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const methods = iface => {
4+
// Introspect all properties of iface object and
5+
// extract function names and number of arguments
6+
// For example: {
7+
// m1: x => [x],
8+
// m2: function (x, y) {
9+
// return [x, y];
10+
// },
11+
// m3(x, y, z) {
12+
// return [x, y, z];
13+
// }
14+
// will return: [
15+
// ['m1', 1],
16+
// ['m2', 2],
17+
// ['m3', 3]
18+
// ]
19+
};
20+
21+
module.exports = { methods };

Exercises/4-methods.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
({
2+
name: 'methods',
3+
length: [180, 220],
4+
cases: [
5+
[
6+
{
7+
m1: x => [x],
8+
m2: function (x, y) {
9+
return [x, y];
10+
},
11+
m3(x, y, z) {
12+
return [x, y, z];
13+
}
14+
}, [
15+
['m1', 1],
16+
['m2', 2],
17+
['m3', 3]
18+
]
19+
],
20+
]
21+
})

Solutions/1-random.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
const random = (min, max) => {
4+
if (max === undefined) {
5+
max = min;
6+
min = 0;
7+
}
8+
return min + Math.floor(Math.random() * (max - min + 1));
9+
};
10+
11+
module.exports = { random };
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ const generateKey = (length, possible) => {
1010
return key;
1111
};
1212

13+
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
14+
const key = generateKey(16, characters);
15+
console.log(key);
16+
1317
module.exports = { generateKey };

0 commit comments

Comments
 (0)