Skip to content

Possible Exercises #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Exercises/2-closure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const coffee = (volume, strength) =>
`Coffee volume: ${volume}ml, strength: ${strength}`;

const defineCoffeeType = volume => strength => coffee(volume, strength);

// Use function defineCoffeeType to define new coffee types.
// Define coffee type espresso which volume should equal 50ml.
// Define coffee type americano which volume should equal 150ml.

const espresso = null;
const americano = null;

module.exports = { espresso, americano };
16 changes: 16 additions & 0 deletions Exercises/2-closure.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[{
name: 'espresso',
length: [30, 70],
cases: [
['medium', 'Coffee volume: 50ml, strength: medium'],
['strong', 'Coffee volume: 50ml, strength: strong'],
]
},
{
name: 'americano',
length: [30, 70],
cases: [
['medium', 'Coffee volume: 150ml, strength: medium'],
['strong', 'Coffee volume: 150ml, strength: strong'],
]
}]
11 changes: 11 additions & 0 deletions Exercises/3-lambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const tagged = (pref, str) => `[${pref}] ${str}`;

// Define function tagDate that prepends current date to the string.
// E.g. tagDate('My String') === '[2019-11-14] My String'
// Use function tagged to implement tagDate.

const tagDate = null;

module.exports = { tagDate };
18 changes: 18 additions & 0 deletions Exercises/3-lambda.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
({
name: 'tagDate',
length: [150, 400],
test: tagDate => {
{
const date = new Date().toISOString().substring(0, 10);
const expected = `[${date}] Test`;
const y = tagDate('Test');
if (y !== expected) {
throw new Error(`tagDate('Test') === ${y} expected: ${expected}`);
}
}
{
const src = tagDate.toString();
if (!src.includes('tagged(')) throw new Error('Use function tagged to implement tagDate');
}
}
})
25 changes: 25 additions & 0 deletions Exercises/4-bind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

/*
Generalized mean (Hölder mean)
Given n numbers a₁, a₂, ... an
Define Hk (for k != 0) as the k-th root of the arithmetic mean
of the k-th power of a set of numbers
____________________________
Hk = ᵏ√ (a₁ᵏ + a₂ᵏ + ... + anᵏ) / n
*/

const H = (exp, ...args) => {
const sum = args.reduce((s, a) => (s + Math.pow(a, exp)), 0);
const avg = sum / args.length;
return Math.pow(avg, (1 / exp));
};

// Use method bind() to create new functions from function H.
// Create function `average` that returns arithmetic mean (H₁) of the arguments.
// Create function `rootMeanSquare` that returns quadratic mean (H₂).

const average = null;
const rootMeanSquare = null;

module.exports = { average, rootMeanSquare };
16 changes: 16 additions & 0 deletions Exercises/4-bind.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
([{
name: 'rootMeanSquare',
length: [10, 70],
cases: [
[7, 1, 5],
[4, 4, 4, 4],
]
},
{
name: 'average',
length: [10, 70],
cases: [
[3, 4, 6, 7, 5],
[4, 4, 4, 4],
]
}])
16 changes: 16 additions & 0 deletions Exercises/5-curry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

// Check 4 digit pin.
const EXPECTED_PIN = '4967';
const checkPin = (...code) => code.join('') === EXPECTED_PIN;

// Define function curry that accepts the length of the function
// (amount of function arguments) and the function.

const curry = (length, fn) => (...args) => null;

// Allows to enter pin code by one character,
// Usage: press('3')('4')('5')('6')
const press = curry(4, checkPin);

module.exports = { press };
42 changes: 42 additions & 0 deletions Exercises/5-curry.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
({
name: 'press',
length: [50, 200],
test: press => {
{
const f1 = press('1');
if (typeof f1 !== 'function') {
throw new Error(`Expected press('1') to be a function.`);
}
}
{
const f2 = press('1')('2');
if (typeof f2 !== 'function') {
throw new Error(`Expected press('1')('2') to be a function.`);
}
}
{
const f3 = press('1')('2')('3');
if (typeof f3 !== 'function') {
throw new Error(`Expected press('1')('2')('3') to be a function.`);
}
}
{
const e4 = press('1')('2')('3')('4');
if (typeof e4 !== 'boolean') {
throw new Error(`Expected press('1')('2')('3')('4') to be a boolean.`);
}
}
{
const res = press('1')('2')('3')('4');
if (res) {
throw new Error(`Expected false when entered wrong pin code.`);
}
}
{
const res = press('4')('9')('6')('7');
if (!res) {
throw new Error(`Expected true when entered correct pin code.`);
}
}
}
})
15 changes: 15 additions & 0 deletions Exercises/6-chaining.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

// Check 4 digit pin.
const EXPECTED_PIN = '4967';
const checkPin = (...code) => code.join('') === EXPECTED_PIN;

// Impement function press
// that allows to enter pin code by one character,
// e.g. press('3').press('4').press('5').press('6')
//
// For hint use https://github.com/HowProgrammingWorks/Cheatsheet

const press = null;

module.exports = { press };
18 changes: 18 additions & 0 deletions Exercises/6-chaining.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
({
name: 'press',
length: [50, 200],
test: press => {
{
const res = press('1').press('2').press('3').press('4');
if (res) {
throw new Error(`Expected false when entered wrong pin code.`);
}
}
{
const res = press('4').press('9').press('6').press('7');
if (!res) {
throw new Error(`Expected true when entered correct pin code.`);
}
}
}
})