diff --git a/Exercises/1-power.js b/Exercises/1-power.js index 9a99509..deb4773 100644 --- a/Exercises/1-power.js +++ b/Exercises/1-power.js @@ -1,15 +1,15 @@ 'use strict'; // Define function power(exp, n), the same as Math.pow(n, exp) -// but with reverse order of argumants. -const power = null; +// but with reverse order of arguments. +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 argumant (to the power of three). -const cube = null; +// The function should return cube of argument (to the power of three). +const cube = (n) => Math.pow.bind(null, n)(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..ee8fb7c 100644 --- a/Exercises/3-lambda.js +++ b/Exercises/3-lambda.js @@ -6,6 +6,9 @@ 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 today = new Date().toISOString().split('T')[0]; + return tagged(today, str); +}; module.exports = { tagDate }; diff --git a/Exercises/4-bind.js b/Exercises/4-bind.js index 1d1849a..3a35601 100644 --- a/Exercises/4-bind.js +++ b/Exercises/4-bind.js @@ -19,7 +19,7 @@ const H = (exp, ...args) => { // 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..51552e5 100644 --- a/Exercises/5-curry.js +++ b/Exercises/5-curry.js @@ -7,7 +7,14 @@ 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; +const curry = (length, fn) => (char) => { + if (length > 1) { + const f = fn.bind(null, char); + return curry(length - 1, f); + } else { + return fn(char); + } +}; // 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 b0b2d26..f26afa5 100644 --- a/Exercises/6-chaining.js +++ b/Exercises/6-chaining.js @@ -10,6 +10,13 @@ const checkPin = (...code) => code.join('') === EXPECTED_PIN; // // For hint use https://github.com/HowProgrammingWorks/Cheatsheet -const press = null; +const press = (n) => ({ + pin: n, + press(sym) { + this.pin += sym; + if (this.pin.length === EXPECTED_PIN.length) return checkPin(this.pin); + return this; + } +}); module.exports = { press };