From 7356ba943ef7588785b914c70ad6afbc763db90b Mon Sep 17 00:00:00 2001 From: Aliaksei Navamlinets Date: Sun, 28 Oct 2018 15:55:46 +0300 Subject: [PATCH 1/5] feat: add implement feature function 'sumOfOther' --- src/sumOfOther.js | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/sumOfOther.js diff --git a/src/sumOfOther.js b/src/sumOfOther.js new file mode 100644 index 0000000..4d01e17 --- /dev/null +++ b/src/sumOfOther.js @@ -0,0 +1,4 @@ +const sumOfOther = (value) => + value.map((e, ind) => + value.reduce((acm, a, i) => + (acm + (ind === i ? 0: a)), 0)); From 40b60b4bbb5c584f697d1321aa77cf53f59f544f Mon Sep 17 00:00:00 2001 From: Aliaksei Navamlinets Date: Sun, 28 Oct 2018 16:22:57 +0300 Subject: [PATCH 2/5] feat: add implement feature function 'make' --- src/make.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/make.js diff --git a/src/make.js b/src/make.js new file mode 100644 index 0000000..f20f7dd --- /dev/null +++ b/src/make.js @@ -0,0 +1,10 @@ +const sum = (a, b) => a + b; +const make = (...args) => { + if (typeof args[0] === 'function') { + const func = args[0]; + return (this.arguments || []).reduce(func); + } + + this.arguments = [...(this.arguments || []), ...args]; + return make; +}; From 941da09338c75078e943cb833c9ee8ca2a2c6d8a Mon Sep 17 00:00:00 2001 From: Aliaksei Navamlinets Date: Sun, 28 Oct 2018 16:27:48 +0300 Subject: [PATCH 3/5] feat: add implement feature function 'recursion' --- src/recursion.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/recursion.js diff --git a/src/recursion.js b/src/recursion.js new file mode 100644 index 0000000..556f39d --- /dev/null +++ b/src/recursion.js @@ -0,0 +1,10 @@ +let result = []; +const recursion = (val, i = 0) => { + result[i] = [...(result[i] || []), val.value]; + + if(val.left) recursion(val.left, i+1); + + if(val.right) recursion(val.right, i+1); + + return result; +} From 616824b372a785e0781567732ce3bc42fd92437c Mon Sep 17 00:00:00 2001 From: Aliaksei Navamlinets Date: Sun, 28 Oct 2018 18:46:12 +0300 Subject: [PATCH 4/5] refactor: add files .gitignore and package.json --- .gitignore | 2 ++ package.json | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 .gitignore create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..25c8fdb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +package-lock.json \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..c862ba2 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "rsschool-codejam", + "version": "1.0.0", + "description": "Code Jam #2", + "main": "test.js", + "scripts": { + "test": "mocha" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/AlexeyNovomlinets/rsschool-codejam.git" + }, + "author": "Alexey Novomlinets ", + "license": "MIT", + "bugs": { + "url": "https://github.com/AlexeyNovomlinets/rsschool-codejam/issues" + }, + "homepage": "https://github.com/AlexeyNovomlinets/rsschool-codejam#readme", + "dependencies": { + "eslint": "^5.8.0", + "mocha": "^5.2.0" + } +} From c52aa468df94e02f31d5d4e0ceb0f9ecd14d05f8 Mon Sep 17 00:00:00 2001 From: Aliaksei Navamlinets Date: Sun, 28 Oct 2018 19:37:54 +0300 Subject: [PATCH 5/5] feat: add unit tests and eslint --- .eslintrc.json | 3 +++ package.json | 10 +++++-- src/make.js | 14 +++++----- src/recursion.js | 17 ++++++------ src/sumOfOther.js | 9 ++++--- test.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 98 insertions(+), 21 deletions(-) create mode 100644 .eslintrc.json create mode 100644 test.js diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..6f67564 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,3 @@ +{ + "extends": "airbnb-base" +} \ No newline at end of file diff --git a/package.json b/package.json index c862ba2..a72e87d 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "description": "Code Jam #2", "main": "test.js", "scripts": { - "test": "mocha" + "test": "mocha", + "start": "node test.js", + "lint": "eslint src" }, "repository": { "type": "git", @@ -17,7 +19,11 @@ }, "homepage": "https://github.com/AlexeyNovomlinets/rsschool-codejam#readme", "dependencies": { - "eslint": "^5.8.0", "mocha": "^5.2.0" + }, + "devDependencies": { + "eslint": "^5.8.0", + "eslint-config-airbnb-base": "^13.1.0", + "eslint-plugin-import": "^2.14.0" } } diff --git a/src/make.js b/src/make.js index f20f7dd..5d4bc90 100644 --- a/src/make.js +++ b/src/make.js @@ -1,10 +1,10 @@ const sum = (a, b) => a + b; const make = (...args) => { - if (typeof args[0] === 'function') { - const func = args[0]; - return (this.arguments || []).reduce(func); - } - - this.arguments = [...(this.arguments || []), ...args]; - return make; + if (typeof args[0] === 'function') { + const func = args[0]; + return (this.arguments || []).reduce(func); + } + this.arguments = [...(this.arguments || []), ...args]; + return make; }; +module.exports = { sum, make }; diff --git a/src/recursion.js b/src/recursion.js index 556f39d..d1406ff 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -1,10 +1,11 @@ -let result = []; -const recursion = (val, i = 0) => { +const rec = (value) => { + const result = []; + const recursion = (val, i = 0) => { result[i] = [...(result[i] || []), val.value]; - - if(val.left) recursion(val.left, i+1); - - if(val.right) recursion(val.right, i+1); - + if (val.left) recursion(val.left, i + 1); + if (val.right) recursion(val.right, i + 1); return result; -} + }; + return recursion(value); +}; +module.exports = rec; diff --git a/src/sumOfOther.js b/src/sumOfOther.js index 4d01e17..bf85428 100644 --- a/src/sumOfOther.js +++ b/src/sumOfOther.js @@ -1,4 +1,5 @@ -const sumOfOther = (value) => - value.map((e, ind) => - value.reduce((acm, a, i) => - (acm + (ind === i ? 0: a)), 0)); +const sumOfOther = (value) => { + if (!Array.isArray(value)) throw new Error('Invalid input!'); + return value.map((e, ind) => value.reduce((acm, a, i) => (acm + (ind === i ? 0 : a)), 0)); +}; +module.exports = sumOfOther; diff --git a/test.js b/test.js new file mode 100644 index 0000000..f426e50 --- /dev/null +++ b/test.js @@ -0,0 +1,66 @@ +const assert = require('assert'); +const sumOfOther = require('./src/sumOfOther'); +const { sum, make } = require('./src/make'); +const recursion = require('./src/recursion'); + +describe('sumOfOther', () => { + it('1', () => { + const res = sumOfOther([2, 3, 4, 1]); + assert.deepEqual(res, [8, 7, 6, 9]); + }); + it('2', () => { + const res = sumOfOther([5, 3, 2, 8, 9, 1]); + assert.deepEqual(res, [23, 25, 26, 20, 19, 27]); + }); + it('3', () => { + const res = sumOfOther([1, 3, 4]); + assert.deepEqual(res, [7, 5, 4]); + }); + it('4', () => { + assert.throws(() => sumOfOther('abc')); + assert.throws(() => sumOfOther(2)); + assert.throws(() => sumOfOther({})); + }); +}); + +describe('make', () => { + it('1', () => { + const res = make(1)(3, 2, 6)(4, 0)(sum); + assert.deepEqual(res, 16); + }); + it('2', () => { + const res = make(1, 2)(3)(4, 1)(sum); + assert.deepEqual(res, 27); + }); + it('3', () => { + const res = make(2, 5, 1, 4)(3, 2)(4, 5)(sum); + assert.deepEqual(res, 53); + }); +}); + +describe('recursion', () => { + it('1', () => { + const res = recursion({ + value: 100, + left: { value: 90, left: { value: 70 }, right: { value: 99 } }, + right: { value: 120, left: { value: 110 }, right: { value: 130 } }, + }); + assert.deepEqual(res, [[100], [90, 120], [70, 99, 110, 130]]); + }); + it('2', () => { + const res = recursion({ + value: 10, + left: { value: 5 }, + right: { value: 12 }, + }); + assert.deepEqual(res, [[10], [5, 12]]); + }); + it('3', () => { + const res = recursion({ + value: 1111, + left: { value: 111, left: { value: 11 }, right: { value: 1111 } }, + right: { value: 11111 }, + }); + assert.deepEqual(res, [[1111], [111, 11111], [11, 1111]]); + }); +});