Skip to content
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

All tasks completed #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions Exercises/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"space-before-function-paren": ["error", "never"]
}
6 changes: 3 additions & 3 deletions Exercises/1-power.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
6 changes: 3 additions & 3 deletions Exercises/2-closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
6 changes: 5 additions & 1 deletion Exercises/3-lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
8 changes: 4 additions & 4 deletions Exercises/4-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
13 changes: 11 additions & 2 deletions Exercises/5-curry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
10 changes: 9 additions & 1 deletion Exercises/6-chaining.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Timur Shemsedinov <[email protected]>",
"license": "MIT",
"scripts": {
"test": "eslint ./Exercises; hpw",
"test": "eslint ./Exercises && hpw",
"ci": "eslint ./Exercises && hpw"
},
"dependencies": {
Expand Down