Skip to content

Commit 17e04e4

Browse files
authored
Exercises (#12)
1 parent 6cd80c4 commit 17e04e4

10 files changed

+192
-0
lines changed

Exercises/2-closure.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const coffee = (volume, strength) =>
4+
`Coffee volume: ${volume}ml, strength: ${strength}`;
5+
6+
const defineCoffeeType = volume => strength => coffee(volume, strength);
7+
8+
// Use function defineCoffeeType to define new coffee types.
9+
// Define coffee type espresso which volume should equal 50ml.
10+
// Define coffee type americano which volume should equal 150ml.
11+
12+
const espresso = null;
13+
const americano = null;
14+
15+
module.exports = { espresso, americano };

Exercises/2-closure.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[{
2+
name: 'espresso',
3+
length: [30, 70],
4+
cases: [
5+
['medium', 'Coffee volume: 50ml, strength: medium'],
6+
['strong', 'Coffee volume: 50ml, strength: strong'],
7+
]
8+
},
9+
{
10+
name: 'americano',
11+
length: [30, 70],
12+
cases: [
13+
['medium', 'Coffee volume: 150ml, strength: medium'],
14+
['strong', 'Coffee volume: 150ml, strength: strong'],
15+
]
16+
}]

Exercises/3-lambda.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 tagged = (pref, str) => `[${pref}] ${str}`;
4+
5+
// Define function tagDate that prepends current date to the string.
6+
// E.g. tagDate('My String') === '[2019-11-14] My String'
7+
// Use function tagged to implement tagDate.
8+
9+
const tagDate = null;
10+
11+
module.exports = { tagDate };

Exercises/3-lambda.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
({
2+
name: 'tagDate',
3+
length: [150, 400],
4+
test: tagDate => {
5+
{
6+
const date = new Date().toISOString().substring(0, 10);
7+
const expected = `[${date}] Test`;
8+
const y = tagDate('Test');
9+
if (y !== expected) {
10+
throw new Error(`tagDate('Test') === ${y} expected: ${expected}`);
11+
}
12+
}
13+
{
14+
const src = tagDate.toString();
15+
if (!src.includes('tagged(')) throw new Error('Use function tagged to implement tagDate');
16+
}
17+
}
18+
})

Exercises/4-bind.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
/*
4+
Generalized mean (Hölder mean)
5+
Given n numbers a₁, a₂, ... an
6+
Define Hk (for k != 0) as the k-th root of the arithmetic mean
7+
of the k-th power of a set of numbers
8+
____________________________
9+
Hk = ᵏ√ (a₁ᵏ + a₂ᵏ + ... + anᵏ) / n
10+
*/
11+
12+
const H = (exp, ...args) => {
13+
const sum = args.reduce((s, a) => (s + Math.pow(a, exp)), 0);
14+
const avg = sum / args.length;
15+
return Math.pow(avg, (1 / exp));
16+
};
17+
18+
// Use method bind() to create new functions from function H.
19+
// Create function `average` that returns arithmetic mean (H₁) of the arguments.
20+
// Create function `rootMeanSquare` that returns quadratic mean (H₂).
21+
22+
const average = null;
23+
const rootMeanSquare = null;
24+
25+
module.exports = { average, rootMeanSquare };

Exercises/4-bind.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
([{
2+
name: 'rootMeanSquare',
3+
length: [10, 70],
4+
cases: [
5+
[7, 1, 5],
6+
[4, 4, 4, 4],
7+
]
8+
},
9+
{
10+
name: 'average',
11+
length: [10, 70],
12+
cases: [
13+
[3, 4, 6, 7, 5],
14+
[4, 4, 4, 4],
15+
]
16+
}])

Exercises/5-curry.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
// Check 4 digit pin.
4+
const EXPECTED_PIN = '4967';
5+
const checkPin = (...code) => code.join('') === EXPECTED_PIN;
6+
7+
// Define function curry that accepts the length of the function
8+
// (amount of function arguments) and the function.
9+
10+
const curry = (length, fn) => (...args) => null;
11+
12+
// Allows to enter pin code by one character,
13+
// Usage: press('3')('4')('5')('6')
14+
const press = curry(4, checkPin);
15+
16+
module.exports = { press };

Exercises/5-curry.test

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
({
2+
name: 'press',
3+
length: [50, 200],
4+
test: press => {
5+
{
6+
const f1 = press('1');
7+
if (typeof f1 !== 'function') {
8+
throw new Error(`Expected press('1') to be a function.`);
9+
}
10+
}
11+
{
12+
const f2 = press('1')('2');
13+
if (typeof f2 !== 'function') {
14+
throw new Error(`Expected press('1')('2') to be a function.`);
15+
}
16+
}
17+
{
18+
const f3 = press('1')('2')('3');
19+
if (typeof f3 !== 'function') {
20+
throw new Error(`Expected press('1')('2')('3') to be a function.`);
21+
}
22+
}
23+
{
24+
const e4 = press('1')('2')('3')('4');
25+
if (typeof e4 !== 'boolean') {
26+
throw new Error(`Expected press('1')('2')('3')('4') to be a boolean.`);
27+
}
28+
}
29+
{
30+
const res = press('1')('2')('3')('4');
31+
if (res) {
32+
throw new Error(`Expected false when entered wrong pin code.`);
33+
}
34+
}
35+
{
36+
const res = press('4')('9')('6')('7');
37+
if (!res) {
38+
throw new Error(`Expected true when entered correct pin code.`);
39+
}
40+
}
41+
}
42+
})

Exercises/6-chaining.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
// Check 4 digit pin.
4+
const EXPECTED_PIN = '4967';
5+
const checkPin = (...code) => code.join('') === EXPECTED_PIN;
6+
7+
// Impement function press
8+
// that allows to enter pin code by one character,
9+
// e.g. press('3').press('4').press('5').press('6')
10+
//
11+
// For hint use https://github.com/HowProgrammingWorks/Cheatsheet
12+
13+
const press = null;
14+
15+
module.exports = { press };

Exercises/6-chaining.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
({
2+
name: 'press',
3+
length: [50, 200],
4+
test: press => {
5+
{
6+
const res = press('1').press('2').press('3').press('4');
7+
if (res) {
8+
throw new Error(`Expected false when entered wrong pin code.`);
9+
}
10+
}
11+
{
12+
const res = press('4').press('9').press('6').press('7');
13+
if (!res) {
14+
throw new Error(`Expected true when entered correct pin code.`);
15+
}
16+
}
17+
}
18+
})

0 commit comments

Comments
 (0)