diff --git a/Exercises/2-closure.js b/Exercises/2-closure.js new file mode 100644 index 0000000..d6f2435 --- /dev/null +++ b/Exercises/2-closure.js @@ -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 }; diff --git a/Exercises/2-closure.test b/Exercises/2-closure.test new file mode 100644 index 0000000..13fcfa1 --- /dev/null +++ b/Exercises/2-closure.test @@ -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'], + ] +}] diff --git a/Exercises/3-lambda.js b/Exercises/3-lambda.js new file mode 100644 index 0000000..994caa8 --- /dev/null +++ b/Exercises/3-lambda.js @@ -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 }; diff --git a/Exercises/3-lambda.test b/Exercises/3-lambda.test new file mode 100644 index 0000000..4347aed --- /dev/null +++ b/Exercises/3-lambda.test @@ -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'); + } + } +}) diff --git a/Exercises/4-bind.js b/Exercises/4-bind.js new file mode 100644 index 0000000..1d1849a --- /dev/null +++ b/Exercises/4-bind.js @@ -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 }; diff --git a/Exercises/4-bind.test b/Exercises/4-bind.test new file mode 100644 index 0000000..c280276 --- /dev/null +++ b/Exercises/4-bind.test @@ -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], + ] +}]) diff --git a/Exercises/5-curry.js b/Exercises/5-curry.js new file mode 100644 index 0000000..e175699 --- /dev/null +++ b/Exercises/5-curry.js @@ -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 }; diff --git a/Exercises/5-curry.test b/Exercises/5-curry.test new file mode 100644 index 0000000..6612626 --- /dev/null +++ b/Exercises/5-curry.test @@ -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.`); + } + } + } +}) diff --git a/Exercises/6-chaining.js b/Exercises/6-chaining.js new file mode 100644 index 0000000..b0b2d26 --- /dev/null +++ b/Exercises/6-chaining.js @@ -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 }; diff --git a/Exercises/6-chaining.test b/Exercises/6-chaining.test new file mode 100644 index 0000000..01d82a8 --- /dev/null +++ b/Exercises/6-chaining.test @@ -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.`); + } + } + } +})