diff --git a/Exercises/.prettierrc b/Exercises/.prettierrc new file mode 100644 index 0000000..15e3873 --- /dev/null +++ b/Exercises/.prettierrc @@ -0,0 +1,7 @@ +{ + "trailingComma": "es5", + "tabWidth": 2, + "semi": true, + "singleQuote": true, + "space-before-function-paren": ["error", "never"] +} diff --git a/Exercises/1-power.js b/Exercises/1-power.js index 83226d2..69be202 100644 --- a/Exercises/1-power.js +++ b/Exercises/1-power.js @@ -2,14 +2,14 @@ // Define function power(exp, n), the same as Math.pow(n, exp) // but with reverse order of arguments. -const power = null; +const power = (exp, n) => Math.pow(n, exp); // Implement function `square(n)`, which returns square of its argument. // The function may or may not reuse function `power`. -const square = null; +const square = (n) => power(2, n); // Implement function `cube(n)` using partial application // The function should return cube of argument (to the power of three). -const cube = null; +const cube = power.bind(null, 3); module.exports = { power, square, cube }; diff --git a/Exercises/2-closure.js b/Exercises/2-closure.js index d6f2435..a419f96 100644 --- a/Exercises/2-closure.js +++ b/Exercises/2-closure.js @@ -3,13 +3,13 @@ const coffee = (volume, strength) => `Coffee volume: ${volume}ml, strength: ${strength}`; -const defineCoffeeType = volume => strength => coffee(volume, 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; +const espresso = defineCoffeeType(50); +const americano = defineCoffeeType(150); module.exports = { espresso, americano }; diff --git a/Exercises/3-lambda.js b/Exercises/3-lambda.js index 994caa8..5fd5aea 100644 --- a/Exercises/3-lambda.js +++ b/Exercises/3-lambda.js @@ -6,6 +6,10 @@ const tagged = (pref, str) => `[${pref}] ${str}`; // E.g. tagDate('My String') === '[2019-11-14] My String' // Use function tagged to implement tagDate. -const tagDate = null; +const tagDate = (str) => { + const currentDate = new Date(); + const formattedDate = currentDate.toISOString().substring(0, 10); + return tagged(formattedDate, str); +}; module.exports = { tagDate }; diff --git a/Exercises/4-bind.js b/Exercises/4-bind.js index 1d1849a..48faf1c 100644 --- a/Exercises/4-bind.js +++ b/Exercises/4-bind.js @@ -10,16 +10,16 @@ */ const H = (exp, ...args) => { - const sum = args.reduce((s, a) => (s + Math.pow(a, exp)), 0); + const sum = args.reduce((s, a) => s + Math.pow(a, exp), 0); const avg = sum / args.length; - return Math.pow(avg, (1 / exp)); + 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; +const average = H.bind(null, 1); +const rootMeanSquare = H.bind(null, 2); module.exports = { average, rootMeanSquare }; diff --git a/Exercises/5-curry.js b/Exercises/5-curry.js index e175699..5078ec7 100644 --- a/Exercises/5-curry.js +++ b/Exercises/5-curry.js @@ -6,8 +6,17 @@ 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; +// prettier-ignore +const curry = + (length, fn) => + (...args) => { + if (length > args.length) { + const f = fn.bind(null, ...args); + return curry(length - 1, f); + } else { + return fn(...args); + } + }; // Allows to enter pin code by one character, // Usage: press('3')('4')('5')('6') diff --git a/Exercises/6-chaining.js b/Exercises/6-chaining.js index e768528..cee2c54 100644 --- a/Exercises/6-chaining.js +++ b/Exercises/6-chaining.js @@ -10,6 +10,14 @@ const checkPin = (...code) => code.join('') === EXPECTED_PIN; // // For hint use https://github.com/HowProgrammingWorks/Cheatsheet -const press = null; +const press = (n) => { + const pin = [n]; + const data = {}; + data.press = (n) => { + pin.push(n); + return pin.length === 4 ? checkPin(...pin) : data; + }; + return data; +}; module.exports = { press }; diff --git a/package.json b/package.json index ebfc0c9..c4de81a 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "author": "Timur Shemsedinov ", "license": "MIT", "scripts": { - "test": "eslint ./Exercises; hpw", + "test": "eslint ./Exercises && hpw", "ci": "eslint ./Exercises && hpw" }, "dependencies": {