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

day10 #123

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

day10 #123

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
3 changes: 3 additions & 0 deletions js-exercises/boolean-expression-evaluator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Instructions

Write a `booleanExpressionEvaluator` method which evaluates boolean expressions. Refer to the test cases for more info.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const possibleValues = ['true', 'false', '!true', '!false', '^', '&', '|'];

const isGarbagevalue = (expression) => {
const expressionArr = expression.split(' ');
return expressionArr.every((value) => possibleValues.includes(value));
};

function booleanExpressionEvaluator(expression) {
if (typeof expression !== 'string') throw Error(`Expected String, received ${typeof expression}`);
if (!isGarbagevalue(expression)) throw Error('Expected the expression to be compose of - true, false, !, ^, &, !');
return !!eval(expression);
}

export { booleanExpressionEvaluator };
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { booleanExpressionEvaluator } from './booleanExpressionEvaluator';

describe('booleanExpressionEvaluator', () => {
it('return the evaulated result', () => {
// simple expressions
expect(booleanExpressionEvaluator('!true')).toEqual(false);
expect(booleanExpressionEvaluator('!false')).toEqual(true);
expect(booleanExpressionEvaluator('false & true')).toEqual(false);
expect(booleanExpressionEvaluator('false & false')).toEqual(false);
expect(booleanExpressionEvaluator('true & true')).toEqual(true);
expect(booleanExpressionEvaluator('true | true')).toEqual(true);
expect(booleanExpressionEvaluator('true | false')).toEqual(true);
expect(booleanExpressionEvaluator('false | false')).toEqual(false);
expect(booleanExpressionEvaluator('true ^ true')).toEqual(false);
expect(booleanExpressionEvaluator('false ^ false')).toEqual(false);

// complex expressions
expect(booleanExpressionEvaluator('true & false | true')).toEqual(true);
expect(booleanExpressionEvaluator('true & !false')).toEqual(true);
expect(booleanExpressionEvaluator('false ^ true')).toEqual(true);
expect(booleanExpressionEvaluator('false ^ true | false')).toEqual(true);
expect(booleanExpressionEvaluator('true ^ true | false')).toEqual(false);
});

it('throws error when expression contains parameters other than true or false', () => {
expect(() => booleanExpressionEvaluator('!(abc)')).toThrow();
expect(() => booleanExpressionEvaluator('true & abc')).toThrow();
expect(() => booleanExpressionEvaluator('true & abc | false')).toThrow();
expect(() => booleanExpressionEvaluator('abc & abc | abc')).toThrow();
});

it('throws error when expression contains parameters other than & or | or ^', () => {
expect(() => booleanExpressionEvaluator('~(true)')).toThrow();
expect(() => booleanExpressionEvaluator('true && abc')).toThrow();
expect(() => booleanExpressionEvaluator('true || false')).toThrow();
expect(() => booleanExpressionEvaluator('true * false')).toThrow();
});

it('throws error when expression is not string', () => {
expect(() => booleanExpressionEvaluator(12)).toThrow();
expect(() => booleanExpressionEvaluator([])).toThrow();
expect(() => booleanExpressionEvaluator({})).toThrow();
expect(() => booleanExpressionEvaluator(NaN)).toThrow();
expect(() => booleanExpressionEvaluator(null)).toThrow();
expect(() => booleanExpressionEvaluator()).toThrow();
});
});
12 changes: 12 additions & 0 deletions js-exercises/curry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Here's the basic usage of the function that you'll be creating:

function add(a, b) {
return a + b;
}
```js
const curriedAdd = curry(add); // <- this is the curry function
console.log( curriedAdd(1)(2) ); // 3
```
See 'curry' tests for further info of the requirement

Read more about it here - [Currying](https://en.wikipedia.org/wiki/Currying)
12 changes: 12 additions & 0 deletions js-exercises/curry/curry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function curry(func) {
return function curried(...args) {
if (args.length >= func.length) {
return func.apply(this, args);
}
return (...args2) => curried.apply(this, args.concat(args2));
};
}

export {
curry,
};
56 changes: 56 additions & 0 deletions js-exercises/curry/curry.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { curry } from './curry';

describe('curry', () => {
test('curries the function at least once', () => {
const add = curry((a, b) => {
return a + b;
});
expect(add(1)(2)).toBe(3);
});

test('curries the function even with a single argument', () => {
const output = curry((n) => {
return n;
});
expect(output(1)).toBe(1);
});

test('curries the function until the arguments needed are given at least once', () => {
const add = curry((a, b, c) => {
return a + b + c;
});
expect(add(1, 2)(3)).toBe(6);
});

test('curries the function until the arguments needed are given multiple times', () => {
const add = curry((a, b, c) => {
return a + b + c;
});
expect(add(1)(2)(3)).toBe(6);
});

test("doesn't share state between calls", () => {
const add = curry((a, b, c) => {
return a + b + c;
});
expect(add(1)(2)(3)).toBe(6);
expect(add(2)(3)(4)).toBe(9);
});

test("doesn't only work with addition", () => {
const merge = curry((a, b, c) => {
return [a, b, c].join(', ');
});
expect(merge('1')(2)(3)).toBe('1, 2, 3');
});

test("doesn't share state between inner calls", () => {
const add = curry((a, b, c, d) => {
return a + b + c + d;
});
const firstTwo = add(1)(2);
expect(firstTwo(3)(4)).toBe(10);
const firstThree = firstTwo(5);
expect(firstThree(6)).toBe(14);
});
});
Empty file added js-exercises/curry/t1.js
Empty file.
3 changes: 3 additions & 0 deletions js-exercises/object-invert/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Instructions

`objectInvert` should return an object where the keys and values have been switched
20 changes: 20 additions & 0 deletions js-exercises/object-invert/objectInvert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const makeObjectFromEntries = arr => Object.assign({}, ...Array.from(arr, ([k, v]) => ({ [k]: v }))); // Object.fromEntries is not supported;
const toEntries = Object.entries;
const map = mapFn => data => data.map(mapFn);
const swap = map(([i, j]) => [j, i]);

const compose = (...fns) => x => fns.reduce((v, f) => f(v), x);


function objectInvert(obj) {
return compose(
toEntries,
swap,
makeObjectFromEntries,
)(obj);
}


export {
objectInvert,
};
21 changes: 21 additions & 0 deletions js-exercises/object-invert/objectInvert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { objectInvert } from './objectInvert';

describe('objectInvert', () => {
it('should return an object', () => {
const obj = {
hi: 'hi',
};
const result = objectInvert(obj);
expect(Array.isArray(result)).toBe(false);
expect(typeof result).toBe('object');
});
it('should return an object where the keys and values have been switched', () => {
const obj = {
x: 'hi',
y: 'sup',
z: 'yo',
};
const result = objectInvert(obj);
expect(result).toEqual({ hi: 'x', sup: 'y', yo: 'z' });
});
});