From 53def7ba24db6563b44d85418e98dbaec020d0f5 Mon Sep 17 00:00:00 2001 From: Ilya Mokin Date: Sun, 3 Apr 2016 16:19:38 +0300 Subject: [PATCH 01/30] async task --- extensions/it-optional.js | 2 +- task/07-yield-tasks.js | 16 +++++++++++++++- test/07-yield-tests.js | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/extensions/it-optional.js b/extensions/it-optional.js index 380b80918..65b17d2a3 100644 --- a/extensions/it-optional.js +++ b/extensions/it-optional.js @@ -6,7 +6,7 @@ function testOptional(title, fn) { it(title, function() { try { - fn.call(this); + return fn.call(this); } catch (err) { if (err.message=="Not implemented") { this.test.skip(); diff --git a/task/07-yield-tasks.js b/task/07-yield-tasks.js index a2369790a..bf0ac2c2d 100644 --- a/task/07-yield-tasks.js +++ b/task/07-yield-tasks.js @@ -129,11 +129,25 @@ function* mergeSortedSequences(source1, source2) { throw new Error('Not implemented'); } +/** + * Resolve Promises and take values step by step. + * + * @params {Iterable.} generator + * @return {Promise} Promise with value returned via return + * + * @example + * async((function*() { var a = yield Promise.resolve(6); return a; }) => 6 + */ +function async(generator) { + throw new Error('Not implemented'); +} + module.exports = { get99BottlesOfBeer: get99BottlesOfBeer, getFibonacciSequence: getFibonacciSequence, depthTraversalTree: depthTraversalTree, breadthTraversalTree: breadthTraversalTree, - mergeSortedSequences: mergeSortedSequences + mergeSortedSequences: mergeSortedSequences, + async : async }; diff --git a/test/07-yield-tests.js b/test/07-yield-tests.js index a38c53e3d..376c4323b 100644 --- a/test/07-yield-tests.js +++ b/test/07-yield-tests.js @@ -446,4 +446,43 @@ describe('07-yield-tasks', function() { assert.equal(count, ITEMS_COUNT); }); + + it.optional('async should resolve Promises and take values step by step', () => { + return new Promise((resolve, reject)=> { + tasks.async(function*() { + let a = yield new Promise((resolve)=> setTimeout(()=>resolve(5), 100)), + b = yield Promise.resolve(6); + assert.equal(a, 5, ''); + assert.equal(b, 6, ''); + + return yield new Promise((resolve)=> resolve(a + b)); + }).then(value=> { + try { + assert.equal(value, 11, ''); + resolve() + } catch (err) { + reject(err); + } + }, (err)=> { + reject(err); + }); + }); + }); + + it.optional('async should handle exception during generator work', () => { + return new Promise((resolve, reject)=> { + tasks.async(function*() { + yield new Promise(()=> {throw new Error("test error");}); + }).then(()=> { + reject(); + }, (err)=> { + try { + assert.equal(err.message, 'test error', ''); + resolve() + } catch (err) { + reject(err); + } + }); + }); + }); }); From 702d9b5ab3eee56e308599f32b291ed4945015a6 Mon Sep 17 00:00:00 2001 From: Ilya Mokin Date: Sun, 3 Apr 2016 16:27:42 +0300 Subject: [PATCH 02/30] async task description --- task/07-yield-tasks.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/task/07-yield-tasks.js b/task/07-yield-tasks.js index bf0ac2c2d..2c7422ee7 100644 --- a/task/07-yield-tasks.js +++ b/task/07-yield-tasks.js @@ -136,7 +136,11 @@ function* mergeSortedSequences(source1, source2) { * @return {Promise} Promise with value returned via return * * @example - * async((function*() { var a = yield Promise.resolve(6); return a; }) => 6 + * async((function*() { + * var a = yield new Promise((resolve)=> setTimeout(()=>resolve(5))); + * var b = yield Promise.resolve(6); + * return a + b; + * }).then(value=>console.log(value)) => 11 */ function async(generator) { throw new Error('Not implemented'); From c47a9b604e2474a1431c67a6310d521a1c693c83 Mon Sep 17 00:00:00 2001 From: Ilya Mokin Date: Sun, 3 Apr 2016 16:44:55 +0300 Subject: [PATCH 03/30] async task, tests fixes --- extensions/it-optional.js | 19 +++++++++++++------ test/07-yield-tests.js | 14 +++++--------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/extensions/it-optional.js b/extensions/it-optional.js index 65b17d2a3..0af4d7756 100644 --- a/extensions/it-optional.js +++ b/extensions/it-optional.js @@ -3,16 +3,23 @@ exports = module.exports = testOptional; function testOptional(title, fn) { - - it(title, function() { - try { - return fn.call(this); - } catch (err) { - if (err.message=="Not implemented") { + + it(title, function () { + let errHandler = (err) => { + if (err.message == "Not implemented") { this.test.skip(); } else { throw err; } + }; + + try { + let promise = fn.call(this); + if (promise && promise.catch) { + return promise.catch(errHandler); + } + } catch (err) { + errHandler(err); } }); diff --git a/test/07-yield-tests.js b/test/07-yield-tests.js index 376c4323b..e22d92638 100644 --- a/test/07-yield-tests.js +++ b/test/07-yield-tests.js @@ -452,13 +452,13 @@ describe('07-yield-tasks', function() { tasks.async(function*() { let a = yield new Promise((resolve)=> setTimeout(()=>resolve(5), 100)), b = yield Promise.resolve(6); - assert.equal(a, 5, ''); - assert.equal(b, 6, ''); + assert.equal(a, 5); + assert.equal(b, 6); return yield new Promise((resolve)=> resolve(a + b)); }).then(value=> { try { - assert.equal(value, 11, ''); + assert.equal(value, 11); resolve() } catch (err) { reject(err); @@ -476,12 +476,8 @@ describe('07-yield-tasks', function() { }).then(()=> { reject(); }, (err)=> { - try { - assert.equal(err.message, 'test error', ''); - resolve() - } catch (err) { - reject(err); - } + if (err.message === 'test error') resolve(); + else reject(err); }); }); }); From 6ea58583b3db95c3f3452d8ba325d7c0c9efe995 Mon Sep 17 00:00:00 2001 From: Ilya Mokin Date: Tue, 12 Sep 2017 23:52:02 +0300 Subject: [PATCH 04/30] added a comment for async task --- task/07-yield-tasks.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/task/07-yield-tasks.js b/task/07-yield-tasks.js index 2c7422ee7..9e88bf9b7 100644 --- a/task/07-yield-tasks.js +++ b/task/07-yield-tasks.js @@ -141,6 +141,8 @@ function* mergeSortedSequences(source1, source2) { * var b = yield Promise.resolve(6); * return a + b; * }).then(value=>console.log(value)) => 11 + * + * Most popular implementation of the logic in npm https://www.npmjs.com/package/co */ function async(generator) { throw new Error('Not implemented'); From dbb7799c8cc027eeea931e5727e6d574ab183188 Mon Sep 17 00:00:00 2001 From: Ilya Mokin Date: Thu, 14 Sep 2017 15:32:58 +0300 Subject: [PATCH 05/30] new timeSpanToString test --- test/03-date-tests.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/03-date-tests.js b/test/03-date-tests.js index 328e57b48..94894e12f 100644 --- a/test/03-date-tests.js +++ b/test/03-date-tests.js @@ -83,6 +83,10 @@ describe('03-date-tasks', function() { startDate: new Date(2000,1,1,10,0,0), endDate: new Date(2000,1,1,15,20,10,453), expected: '05:20:10.453' + }, { + startDate: new Date(2000,1,1,10,0,0), + endDate: new Date(2000,1,2,15,20,10,453), + expected: '29:20:10.453' } ].forEach(data => { assert.equal( From b1392c051076282ce81506b6cb61841d8348187d Mon Sep 17 00:00:00 2001 From: Ilya Mokin Date: Sat, 16 Sep 2017 18:41:48 +0300 Subject: [PATCH 06/30] additional getFactorial test --- test/06-conditions-n-loops-tests.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/06-conditions-n-loops-tests.js b/test/06-conditions-n-loops-tests.js index 93ffa3eee..4d9ffe701 100644 --- a/test/06-conditions-n-loops-tests.js +++ b/test/06-conditions-n-loops-tests.js @@ -63,7 +63,8 @@ describe('06-conditions-n-loops-tasks', function() { [ { n: 1, expected: 1 }, { n: 5, expected: 120 }, - { n: 10, expected: 3628800 } + { n: 10, expected: 3628800 }, + { n: 41758, expected: Number.POSITIVE_INFINITY } ].forEach(data => { var actual = tasks.getFactorial(data.n); assert.equal( From c015aaee6849f00a5f60436b91777b266ae0d9b0 Mon Sep 17 00:00:00 2001 From: Ilya Mokin Date: Sat, 16 Sep 2017 19:24:25 +0300 Subject: [PATCH 07/30] depthTraversalTree test, timeout increase --- test/07-yield-tests.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/07-yield-tests.js b/test/07-yield-tests.js index e22d92638..113859c8f 100644 --- a/test/07-yield-tests.js +++ b/test/07-yield-tests.js @@ -316,6 +316,7 @@ describe('07-yield-tasks', function() { }); it.optional('depthTraversalTree should process a wide tree', () => { + this.timeout(30000); var root = createWideTree(); var index = 1; for(let node of tasks.depthTraversalTree(root)) { From 40d6ba40e5304286535ee3962807fed7fa4cffb9 Mon Sep 17 00:00:00 2001 From: Ilya Mokin Date: Thu, 21 Sep 2017 10:43:05 +0300 Subject: [PATCH 08/30] async additional test --- test/07-yield-tests.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/07-yield-tests.js b/test/07-yield-tests.js index 113859c8f..62005cfac 100644 --- a/test/07-yield-tests.js +++ b/test/07-yield-tests.js @@ -470,6 +470,29 @@ describe('07-yield-tasks', function() { }); }); + it.optional('async should resolve Promises and take values step by step', () => { + return new Promise((resolve, reject)=> { + tasks.async(function*() { + let a = yield new Promise((resolve)=> setTimeout(()=>resolve(5), 100)), + b = yield Promise.resolve(6), + c = yield Promise.resolve(6); + assert.equal(a, 5); + assert.equal(b, 6); + assert.equal(c, 6); + return yield new Promise((resolve)=> resolve(a + b + c)); + }).then(value=> { + try { + assert.equal(value, 17); + resolve() + } catch (err) { + reject(err); + } + }, (err)=> { + reject(err); + }); + }); + }); + it.optional('async should handle exception during generator work', () => { return new Promise((resolve, reject)=> { tasks.async(function*() { From df9bce06ef36fd3f2fa38eda0d52faa708268e57 Mon Sep 17 00:00:00 2001 From: Vadim Date: Fri, 22 Sep 2017 09:52:35 +0300 Subject: [PATCH 09/30] additional test for domino task --- test/10-katas-1-tests.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/10-katas-1-tests.js b/test/10-katas-1-tests.js index 0d9023da6..bf420882e 100644 --- a/test/10-katas-1-tests.js +++ b/test/10-katas-1-tests.js @@ -162,6 +162,8 @@ describe('10-katas-1-tasks', function() { [1,3], [2,3], [1,4], [2,4], [1,5], [2,5] ],[ [1,1], [1,2], [2,3], [2,5], [2,6], [3,6], [5,6], [6,6] + ],[ + [1,2], [2,1], [2,2] ] ].forEach(data => { var actual = tasks.canDominoesMakeRow(data); From 96f0d13e07a8fe941d6867b47c437424f4d834b3 Mon Sep 17 00:00:00 2001 From: Vadim Date: Tue, 26 Sep 2017 16:26:37 +0300 Subject: [PATCH 10/30] additional test to retry task --- test/09-functions-n-closures-tests.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/09-functions-n-closures-tests.js b/test/09-functions-n-closures-tests.js index 595713a90..e62f69d59 100644 --- a/test/09-functions-n-closures-tests.js +++ b/test/09-functions-n-closures-tests.js @@ -88,6 +88,20 @@ describe('09-functions-n-closures-tasks', function() { }); + it.optional('retry method should throw error when attemps is end', () => { + var maxAttemps = 3; + var attemps = 0; + var expected = 'expected'; + + var fn = function() { + if (++attemps { var log = ''; From 323599e2d584d2deb24f4d88b038bb4caabaabdd Mon Sep 17 00:00:00 2001 From: Vadim Date: Tue, 26 Sep 2017 16:38:45 +0300 Subject: [PATCH 11/30] fix --- test/09-functions-n-closures-tests.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/09-functions-n-closures-tests.js b/test/09-functions-n-closures-tests.js index e62f69d59..944476241 100644 --- a/test/09-functions-n-closures-tests.js +++ b/test/09-functions-n-closures-tests.js @@ -88,14 +88,13 @@ describe('09-functions-n-closures-tasks', function() { }); - it.optional('retry method should throw error when attemps is end', () => { + it.optional('retry method should throw error when attemps are end', () => { var maxAttemps = 3; var attemps = 0; var expected = 'expected'; var fn = function() { - if (++attemps Date: Tue, 26 Sep 2017 16:45:59 +0300 Subject: [PATCH 12/30] fix --- test/09-functions-n-closures-tests.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/09-functions-n-closures-tests.js b/test/09-functions-n-closures-tests.js index 944476241..595576413 100644 --- a/test/09-functions-n-closures-tests.js +++ b/test/09-functions-n-closures-tests.js @@ -88,10 +88,8 @@ describe('09-functions-n-closures-tasks', function() { }); - it.optional('retry method should throw error when attemps are end', () => { + it.optional('retry method should throw an error when attemps are end', () => { var maxAttemps = 3; - var attemps = 0; - var expected = 'expected'; var fn = function() { throw new Error(); From b57423934ff4ac53cf16543b216058d7cf8839e3 Mon Sep 17 00:00:00 2001 From: Vadim Date: Tue, 26 Sep 2017 18:00:32 +0300 Subject: [PATCH 13/30] retry test fix --- test/09-functions-n-closures-tests.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/09-functions-n-closures-tests.js b/test/09-functions-n-closures-tests.js index 595576413..6460d56b6 100644 --- a/test/09-functions-n-closures-tests.js +++ b/test/09-functions-n-closures-tests.js @@ -92,10 +92,16 @@ describe('09-functions-n-closures-tasks', function() { var maxAttemps = 3; var fn = function() { - throw new Error(); + throw new Error("fulyError"); } - + assert.throws(tasks.retry(fn, maxAttemps), Error); + + try { + tasks.retry(fn, maxAttemps)() + } catch (err) { + assert.equal(err.message, "fulyError", "retry method should throw an initial error"); + }; }); From 43cf153ca8b13759711459feb1ffc5a0c5878908 Mon Sep 17 00:00:00 2001 From: Vadim Date: Tue, 26 Sep 2017 18:33:39 +0300 Subject: [PATCH 14/30] fix --- test/09-functions-n-closures-tests.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/test/09-functions-n-closures-tests.js b/test/09-functions-n-closures-tests.js index 6460d56b6..7b5a0dbf8 100644 --- a/test/09-functions-n-closures-tests.js +++ b/test/09-functions-n-closures-tests.js @@ -95,13 +95,10 @@ describe('09-functions-n-closures-tasks', function() { throw new Error("fulyError"); } - assert.throws(tasks.retry(fn, maxAttemps), Error); - - try { - tasks.retry(fn, maxAttemps)() - } catch (err) { + assert.throws(tasks.retry(fn, maxAttemps), function (err) { assert.equal(err.message, "fulyError", "retry method should throw an initial error"); - }; + return true; + }); }); From 827e2c9097a1d272dc79952c8887309467f7940f Mon Sep 17 00:00:00 2001 From: Ilya Mokin Date: Mon, 29 Jun 2020 21:40:12 +0300 Subject: [PATCH 15/30] travis --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a81b5c08a..79f9dff75 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ [![Brest Rolling Scopes](http://brest.rollingscopes.com/images/logo_rs_text.svg)](http://brest.rollingscopes.com/) #Brest Rolling Scopes School -## Javascript Assignments [![Build Status](https://travis-ci.org/rolling-scopes-school/js-assignments.svg?branch=master)](https://travis-ci.org/rolling-scopes-school/js-assignments) +## Javascript Assignments [![Build Status](https://travis-ci.org/AisBrestEDU/js-assignments.svg?branch=master)](https://travis-ci.org/AisBrestEDU/js-assignments) Yet another javascript assignments. There are a lot of interactive javascript resources for beginners, but most of them are online and do not cover the modern programming workflow. There are some excellent training resources on github (https://github.com/rmurphey/js-assessment, https://github.com/mrdavidlaing/javascript-koans, https://github.com/vasanthk/js-bits etc) but they are not exactly simulate the everyday programming process. So the motivation of this project is to show TDD process in the wild to the beginners. Assingment tests are implemented in various ways to feel a difference and gain the experience what manner is good, what is bad and what is ugly. From 9e6cd2679d98b5ebaeea1716ebded2e7bff23fea Mon Sep 17 00:00:00 2001 From: tapeghad Date: Tue, 30 Jun 2020 19:20:28 +0300 Subject: [PATCH 16/30] Update the links --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 79f9dff75..775a6a210 100644 --- a/README.md +++ b/README.md @@ -22,14 +22,14 @@ To start javascript assignments please follow the next steps: ### How to setup travis-ci * Open [https://travis-ci.org/](https://travis-ci.org/) and sign in with your github account. * Activate your forked repo **js-assignments**. -* Edit local README.md file and update all links (just replace all occurrences of `'rolling-scopes-school'` with your account name). +* Edit local README.md file and update all links (just replace all occurrences of `'TapeGhad'` with your account name). * Commit and push updated README.md to github: ```bash git add README.md git commit -m "Update the links" git push origin master ``` -* Open https://github.com/rolling-scopes-school/js-assignments and test the build icon. Now it will run all tests and update status once you push changes to github. Keep this icon green! +* Open https://github.com/TapeGhad/js-assignments and test the build icon. Now it will run all tests and update status once you push changes to github. Keep this icon green! ### How to setup work environment @@ -73,7 +73,7 @@ and run the unit tests again. Find one test failed (red). Now it's time to fix i * Implement the function by any way and verify your solution by running tests until the failed test become passed (green). * Your solution work, but now time to refactor it. Try to make your code as pretty and simple as possible keeping up the test green. * Once you can't improve your code and tests are passed you can commit your solution. -* Push your updates to github server and check if tests passed on [travis-ci](https://travis-ci.org/rolling-scopes-school/js-assignments/builds). +* Push your updates to github server and check if tests passed on [travis-ci](https://travis-ci.org/TapeGhad/js-assignments/builds). * If everything is OK you can try to resolve the next task. ### How to debug tasks From 79a2f5a6c79ba7dabc825c038cd38e6a18bfcd9d Mon Sep 17 00:00:00 2001 From: tapeghad Date: Fri, 3 Jul 2020 18:17:16 +0300 Subject: [PATCH 17/30] task1-4 --- launch.json | 11 +++ package-lock.json | 154 ++++++++++++++++++++++++++++++++++++ package.json | 8 +- task/01-strings-tasks.js | 54 +++++++++---- task/02-numbers-tasks.js | 40 +++++++--- task/03-date-tasks.js | 27 +++++-- task/04-arrays-tasks.js | 163 +++++++++++++++++++++++++++++++-------- 7 files changed, 390 insertions(+), 67 deletions(-) create mode 100644 launch.json create mode 100644 package-lock.json diff --git a/launch.json b/launch.json new file mode 100644 index 000000000..f72fbfcf7 --- /dev/null +++ b/launch.json @@ -0,0 +1,11 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha", + } + ] + } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 000000000..9c3bb9263 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,154 @@ +{ + "name": "js-training", + "version": "0.9.1", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "commander": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.3.0.tgz", + "integrity": "sha1-/UMOiJgy7DU7ms0d4hfBHLPu+HM=", + "dev": true + }, + "debug": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.2.0.tgz", + "integrity": "sha1-+HBX6ZWxofauaklgZkE3vFbwOdo=", + "dev": true, + "requires": { + "ms": "0.7.1" + } + }, + "diff": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-1.4.0.tgz", + "integrity": "sha1-fyjS657nsVqX79ic5j3P2qPMur8=", + "dev": true + }, + "escape-string-regexp": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.2.tgz", + "integrity": "sha1-Tbwv5nTnGUnK8/smlc5/LcHZqNE=", + "dev": true + }, + "glob": { + "version": "3.2.11", + "resolved": "https://registry.npmjs.org/glob/-/glob-3.2.11.tgz", + "integrity": "sha1-Spc/Y1uRkPcV0QmH1cAP0oFevj0=", + "dev": true, + "requires": { + "inherits": "2", + "minimatch": "0.3" + } + }, + "growl": { + "version": "1.9.2", + "resolved": "https://registry.npmjs.org/growl/-/growl-1.9.2.tgz", + "integrity": "sha1-Dqd0NxXbjY3ixe3hd14bRayFwC8=", + "dev": true + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true + }, + "jade": { + "version": "0.26.3", + "resolved": "https://registry.npmjs.org/jade/-/jade-0.26.3.tgz", + "integrity": "sha1-jxDXl32NefL2/4YqgbBRPMslaGw=", + "dev": true, + "requires": { + "commander": "0.6.1", + "mkdirp": "0.3.0" + }, + "dependencies": { + "commander": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-0.6.1.tgz", + "integrity": "sha1-+mihT2qUXVTbvlDYzbMyDp47GgY=", + "dev": true + }, + "mkdirp": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.0.tgz", + "integrity": "sha1-G79asbqCevI1dRQ0kEJkVfSB/h4=", + "dev": true + } + } + }, + "lru-cache": { + "version": "2.7.3", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-2.7.3.tgz", + "integrity": "sha1-bUUk6LlV+V1PW1iFHOId1y+06VI=", + "dev": true + }, + "minimatch": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-0.3.0.tgz", + "integrity": "sha1-J12O2qxPG7MyZHIInnlJyDlGmd0=", + "dev": true, + "requires": { + "lru-cache": "2", + "sigmund": "~1.0.0" + } + }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "dev": true, + "requires": { + "minimist": "0.0.8" + } + }, + "mocha": { + "version": "2.5.3", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-2.5.3.tgz", + "integrity": "sha1-FhvlvetJZ3HrmzV0UFC2IrWu/Fg=", + "dev": true, + "requires": { + "commander": "2.3.0", + "debug": "2.2.0", + "diff": "1.4.0", + "escape-string-regexp": "1.0.2", + "glob": "3.2.11", + "growl": "1.9.2", + "jade": "0.26.3", + "mkdirp": "0.5.1", + "supports-color": "1.2.0", + "to-iso-string": "0.0.2" + } + }, + "ms": { + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/ms/-/ms-0.7.1.tgz", + "integrity": "sha1-nNE8A62/8ltl7/3nzoZO6VIBcJg=", + "dev": true + }, + "sigmund": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/sigmund/-/sigmund-1.0.1.tgz", + "integrity": "sha1-P/IfGYytIXX587eBhT/ZTQ0ZtZA=", + "dev": true + }, + "supports-color": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-1.2.0.tgz", + "integrity": "sha1-/x7R5hFp0Gs88tWI4YixjYhH4X4=", + "dev": true + }, + "to-iso-string": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/to-iso-string/-/to-iso-string-0.0.2.tgz", + "integrity": "sha1-TcGeZk38y+Jb2NtQiwDG2hWCVdE=", + "dev": true + } + } +} diff --git a/package.json b/package.json index 712b264fa..38276dce3 100644 --- a/package.json +++ b/package.json @@ -9,10 +9,10 @@ "author": "aorgish", "license": "MIT", "devDependencies": { - "mocha": "^2.3.4" + "mocha": "^2.5.3" }, - "repository" : { - "type" : "git", - "url" : "https://github.com/rolling-scopes-school/js-assignments.git" + "repository": { + "type": "git", + "url": "https://github.com/rolling-scopes-school/js-assignments.git" } } diff --git a/task/01-strings-tasks.js b/task/01-strings-tasks.js index e28054657..4a0263784 100644 --- a/task/01-strings-tasks.js +++ b/task/01-strings-tasks.js @@ -22,7 +22,7 @@ * '', 'bb' => 'bb' */ function concatenateStrings(value1, value2) { - throw new Error('Not implemented'); + return value1+value2 } @@ -38,7 +38,7 @@ function concatenateStrings(value1, value2) { * '' => 0 */ function getStringLength(value) { - throw new Error('Not implemented'); + return value.length } /** @@ -55,7 +55,7 @@ function getStringLength(value) { * 'Chuck','Norris' => 'Hello, Chuck Norris!' */ function getStringFromTemplate(firstName, lastName) { - throw new Error('Not implemented'); + return `Hello, ${firstName} ${lastName}!` } /** @@ -69,7 +69,8 @@ function getStringFromTemplate(firstName, lastName) { * 'Hello, Chuck Norris!' => 'Chuck Norris' */ function extractNameFromTemplate(value) { - throw new Error('Not implemented'); + let temp = value.slice(7) + return temp.slice(0,-1) } @@ -84,7 +85,7 @@ function extractNameFromTemplate(value) { * 'cat' => 'c' */ function getFirstChar(value) { - throw new Error('Not implemented'); + return value[0] } /** @@ -99,7 +100,7 @@ function getFirstChar(value) { * '\tHello, World! ' => 'Hello, World!' */ function removeLeadingAndTrailingWhitespaces(value) { - throw new Error('Not implemented'); + return value.trim(/\t| |/g,'') } /** @@ -114,7 +115,7 @@ function removeLeadingAndTrailingWhitespaces(value) { * 'cat', 3 => 'catcatcat' */ function repeatString(value, count) { - throw new Error('Not implemented'); + return value.repeat(count) } /** @@ -130,7 +131,7 @@ function repeatString(value, count) { * 'ABABAB','BA' => 'ABAB' */ function removeFirstOccurrences(str, value) { - throw new Error('Not implemented'); + return str.replace(value, '') } /** @@ -145,7 +146,7 @@ function removeFirstOccurrences(str, value) { * '' => 'a' */ function unbracketTag(str) { - throw new Error('Not implemented'); + return str.replace(/<|>|/g,'') } @@ -160,7 +161,7 @@ function unbracketTag(str) { * 'abcdefghijklmnopqrstuvwxyz' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' */ function convertToUpperCase(str) { - throw new Error('Not implemented'); + return str.toUpperCase() } /** @@ -174,7 +175,7 @@ function convertToUpperCase(str) { * 'info@gmail.com' => ['info@gmail.com'] */ function extractEmails(str) { - throw new Error('Not implemented'); + return str.split(";") } /** @@ -201,7 +202,14 @@ function extractEmails(str) { * */ function getRectangleString(width, height) { - throw new Error('Not implemented'); + let str="┌"+"─".repeat(width-2)+"┐"+'\n' + let count=height-2 + while (count>0) + { + str = str.concat("│"+" ".repeat(width-2))+"│\n" + count-- + } + return str.concat("└"+"─".repeat(width-2)+"┘\n") } @@ -221,7 +229,18 @@ function getRectangleString(width, height) { * */ function encodeToRot13(str) { - throw new Error('Not implemented'); + let arr=[], count=0 + while (count=78 & str.charCodeAt(count) <=90)| (str.charCodeAt(count) >=110 & str.charCodeAt(count)<=122)) + arr.push(str.charCodeAt(count)-13) + else if ((str.charCodeAt(count) >=65 & str.charCodeAt(count) <=77)| (str.charCodeAt(count) >=97 & str.charCodeAt(count)<=109)) + arr.push(str.charCodeAt(count)+13) + else arr.push(str.charCodeAt(count)) + count++ + + } + return String.fromCharCode(...arr) } /** @@ -238,7 +257,8 @@ function encodeToRot13(str) { * isString(new String('test')) => true */ function isString(value) { - throw new Error('Not implemented'); + if (typeof value ==="string" || value instanceof String) return true + else return false } @@ -267,7 +287,11 @@ function isString(value) { * 'K♠' => 51 */ function getCardId(value) { - throw new Error('Not implemented'); + let arr=['A♣','2♣','3♣','4♣','5♣','6♣','7♣','8♣','9♣','10♣','J♣','Q♣','K♣', + 'A♦','2♦','3♦','4♦','5♦','6♦','7♦','8♦','9♦','10♦','J♦','Q♦','K♦', + 'A♥','2♥','3♥','4♥','5♥','6♥','7♥','8♥','9♥','10♥','J♥','Q♥','K♥', + 'A♠','2♠','3♠','4♠','5♠','6♠','7♠','8♠','9♠','10♠','J♠','Q♠','K♠'] + return arr.indexOf(value) } diff --git a/task/02-numbers-tasks.js b/task/02-numbers-tasks.js index c9ed20208..508c0b846 100644 --- a/task/02-numbers-tasks.js +++ b/task/02-numbers-tasks.js @@ -1,5 +1,7 @@ 'use strict'; +const { interfaces } = require("mocha"); + /******************************************************************************************** * * * Plese read the following tutorial before implementing tasks: * @@ -22,7 +24,7 @@ * 5, 5 => 25 */ function getRectangleArea(width, height) { - throw new Error('Not implemented'); + return width*height } @@ -38,7 +40,7 @@ function getRectangleArea(width, height) { * 0 => 0 */ function getCicleCircumference(radius) { - throw new Error('Not implemented'); + return 2*radius*Math.PI } /** @@ -54,7 +56,8 @@ function getCicleCircumference(radius) { * -3, 3 => 0 */ function getAverage(value1, value2) { - throw new Error('Not implemented'); + return value1/2 +value2/2 + //throw new Error('Not implemented'); } /** @@ -73,7 +76,7 @@ function getAverage(value1, value2) { * (-5,0) (10,-10) => 18.027756377319946 */ function getDistanceBetweenPoints(x1, y1, x2, y2) { - throw new Error('Not implemented'); + return Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2)) } /** @@ -89,7 +92,7 @@ function getDistanceBetweenPoints(x1, y1, x2, y2) { * 5*x = 0 => 0 */ function getLinearEquationRoot(a, b) { - throw new Error('Not implemented'); + return -b/a } @@ -111,7 +114,7 @@ function getLinearEquationRoot(a, b) { * (0,1) (1,2) => 0 */ function getAngleBetweenVectors(x1, y1, x2, y2) { - throw new Error('Not implemented'); + return Math.acos((x1*x2+y1*y2)/(Math.sqrt(Math.pow(x1,2)+Math.pow(y1,2)) * Math.sqrt(Math.sqrt(Math.pow(x2,2)+Math.pow(y2,2))))) } /** @@ -127,7 +130,7 @@ function getAngleBetweenVectors(x1, y1, x2, y2) { * 0 => 0 */ function getLastDigit(value) { - throw new Error('Not implemented'); + return value%10 } @@ -143,7 +146,7 @@ function getLastDigit(value) { * '-525.5' => -525.5 */ function parseNumberFromString(value) { - throw new Error('Not implemented'); + return parseFloat(value) } /** @@ -160,7 +163,7 @@ function parseNumberFromString(value) { * 1,2,3 => 3.741657386773941 */ function getParallelipidedDiagonal(a,b,c) { - throw new Error('Not implemented'); + return Math.sqrt(Math.pow(a,2) +Math.pow(b,2)+Math.pow(c,2)) } /** @@ -181,7 +184,8 @@ function getParallelipidedDiagonal(a,b,c) { * 1678, 3 => 2000 */ function roundToPowerOfTen(num, pow) { - throw new Error('Not implemented'); + return Math.round(num/10**pow)*10**pow + } /** @@ -202,7 +206,16 @@ function roundToPowerOfTen(num, pow) { * 17 => true */ function isPrime(n) { - throw new Error('Not implemented'); + let check=true, count=2 + while (count <=n/2){ + if (n%count===0) + {check = false; + break + } + count++ + } + if (check===true) return true + else return false } /** @@ -221,7 +234,10 @@ function isPrime(n) { * toNumber(new Number(42), 0) => 42 */ function toNumber(value, def) { - throw new Error('Not implemented'); + if (parseInt(value)) return parseInt(value) + return def + //throw new Error('Not implemented'); + } module.exports = { diff --git a/task/03-date-tasks.js b/task/03-date-tasks.js index 83c6266bc..dce30e3fe 100644 --- a/task/03-date-tasks.js +++ b/task/03-date-tasks.js @@ -22,7 +22,7 @@ * 'Sun, 17 May 1998 03:00:00 GMT+01' => Date() */ function parseDataFromRfc2822(value) { - throw new Error('Not implemented'); + return Date.parse(value) } /** @@ -37,7 +37,7 @@ function parseDataFromRfc2822(value) { * '2016-01-19T08:07:37Z' => Date() */ function parseDataFromIso8601(value) { - throw new Error('Not implemented'); + return Date.parse(value) } @@ -56,7 +56,10 @@ function parseDataFromIso8601(value) { * Date(2015,1,1) => false */ function isLeapYear(date) { - throw new Error('Not implemented'); + if (date.getFullYear()%4!=0) return false + else if (date.getFullYear()%100!=0) return true + else if (date.getFullYear()%400!=0) return false + else return true } @@ -76,7 +79,20 @@ function isLeapYear(date) { * Date(2000,1,1,10,0,0), Date(2000,1,1,15,20,10,453) => "05:20:10.453" */ function timeSpanToString(startDate, endDate) { - throw new Error('Not implemented'); + let time_p=endDate-startDate + let milliseconds = parseInt((time_p % 1000)), + seconds = Math.floor((time_p / 1000) % 60), + minutes = Math.floor((time_p / (1000 * 60)) % 60), + hours = Math.floor((time_p / (1000 * 60 * 60)) % 24); + hours = (startDate.getDay()!== endDate.getDay()) ? hours+24 : hours; + hours = (hours < 10) ? "0" + hours : hours; + minutes = (minutes < 10) ? "0" + minutes : minutes; + seconds = (seconds < 10) ? "0" + seconds : seconds; + milliseconds = (milliseconds < 100) ? "0" + milliseconds : milliseconds; + milliseconds = (milliseconds < 10) ? "0" + milliseconds : milliseconds; + + return hours + ":" + minutes + ":" + seconds + "." + milliseconds +//throw new Error('Not implemented'); } @@ -94,7 +110,8 @@ function timeSpanToString(startDate, endDate) { * Date.UTC(2016,3,5,21, 0) => Math.PI/2 */ function angleBetweenClockHands(date) { - throw new Error('Not implemented'); + let total = date.getHours()-3>12? 0.5*(60*(date.getHours()-15)-11*date.getMinutes()):0.5*(60*(date.getHours()-3)-11*date.getMinutes()) + return Math.abs(total)>180 ? (360-Math.abs(total))*Math.PI/180:Math.abs(total)*Math.PI/180 } diff --git a/task/04-arrays-tasks.js b/task/04-arrays-tasks.js index ff3a4c019..e2e4c3a8b 100644 --- a/task/04-arrays-tasks.js +++ b/task/04-arrays-tasks.js @@ -23,7 +23,8 @@ * [0, 1, 2, 3, 4, 5], 5 => 5 */ function findElement(arr, value) { - throw new Error('Not implemented'); + if (arr.includes(value)) return arr.indexOf(value) + else return -1 } /** @@ -38,7 +39,13 @@ function findElement(arr, value) { * 5 => [ 1, 3, 5, 7, 9 ] */ function generateOdds(len) { - throw new Error('Not implemented'); + let arr=[], count=1 + while (len>0){ + arr.push(count); + count+=2 + len-- + } + return arr } @@ -54,7 +61,7 @@ function generateOdds(len) { * [] => [] */ function doubleArray(arr) { - throw new Error('Not implemented'); + return arr.concat(arr) } @@ -70,7 +77,9 @@ function doubleArray(arr) { * [] => [] */ function getArrayOfPositives(arr) { - throw new Error('Not implemented'); + return arr.filter(elem =>{ + return elem>0 + }) } /** @@ -85,7 +94,9 @@ function getArrayOfPositives(arr) { * [ 'cat, 'dog', 'raccon' ] => [ 'cat', 'dog', 'racoon' ] */ function getArrayOfStrings(arr) { - throw new Error('Not implemented'); + return arr.filter(elem =>{ + return typeof elem ==="string" + }) } /** @@ -102,7 +113,9 @@ function getArrayOfStrings(arr) { * [ false, 0, NaN, '', undefined ] => [ ] */ function removeFalsyValues(arr) { - throw new Error('Not implemented'); + return arr.filter(elem =>{ + if (elem) return elem + }) } /** @@ -116,7 +129,9 @@ function removeFalsyValues(arr) { * [ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ] => [ 'A', 'B', 'C', 'D', 'E', 'F', 'G' ] */ function getUpperCaseStrings(arr) { - throw new Error('Not implemented'); + return arr.map(elem =>{ + return elem.toUpperCase() + }) } @@ -131,7 +146,9 @@ function getUpperCaseStrings(arr) { * [ 'angular', 'react', 'ember' ] => [ 7, 5, 5 ] */ function getStringsLength(arr) { - throw new Error('Not implemented'); + return arr.map(elem =>{ + return elem.length + }) } /** @@ -146,7 +163,7 @@ function getStringsLength(arr) { * [ 1, 'b', 'c'], 0, 'x' => [ 'x', 1, 'b', 'c' ] */ function insertItem(arr, item, index) { - throw new Error('Not implemented'); + return arr.splice(index, 0, item) } /** @@ -160,7 +177,7 @@ function insertItem(arr, item, index) { * [ 'a', 'b', 'c', 'd'], 3 => [ 'a', 'b', 'c' ] */ function getHead(arr, n) { - throw new Error('Not implemented'); + return arr.slice(0, n) } @@ -175,7 +192,7 @@ function getHead(arr, n) { * [ 'a', 'b', 'c', 'd'], 3 => [ 'b', 'c', 'd' ] */ function getTail(arr, n) { - throw new Error('Not implemented'); + return arr.slice(-n) } @@ -200,7 +217,7 @@ function getTail(arr, n) { * +'30,31,32,33,34' */ function toCsvText(arr) { - throw new Error('Not implemented'); + return arr.join("\n") } /** @@ -215,7 +232,7 @@ function toCsvText(arr) { * [ 10, 100, -1 ] => [ 100, 10000, 1 ] */ function toArrayOfSquares(arr) { - throw new Error('Not implemented'); + return arr.map(elem=>{return Math.pow(elem, 2)}) } @@ -234,7 +251,12 @@ function toArrayOfSquares(arr) { * [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] => [ 1, 3, 6, 10, 15, 21, 28, 36, 45, 55 ] */ function getMovingSum(arr) { - throw new Error('Not implemented'); + let newArr=[] + arr.reduce(function(sum, current) { + newArr.push(sum+current); + return sum + current; + },0); + return newArr } /** @@ -249,7 +271,9 @@ function getMovingSum(arr) { * [ "a" ] => [] */ function getSecondItems(arr) { - throw new Error('Not implemented'); + return arr.filter(elem=>{ + return arr.indexOf(elem)%2===1 + }) } @@ -268,7 +292,15 @@ function getSecondItems(arr) { * [ 1,2,3,4,5 ] => [ 1, 2,2, 3,3,3, 4,4,4,4, 5,5,5,5,5 ] */ function propagateItemsByPositionIndex(arr) { - throw new Error('Not implemented'); + let newArr=[] + arr.map(elem=>{ + let count=arr.indexOf(elem)+1 + while (count>0){ + newArr.push(elem) + count-- + } + }) + return newArr } @@ -286,7 +318,7 @@ function propagateItemsByPositionIndex(arr) { * [ 10, 10, 10, 10 ] => [ 10, 10, 10 ] */ function get3TopItems(arr) { - throw new Error('Not implemented'); + return arr.sort((a, b)=>{return b-a}).slice(0,3); } @@ -304,7 +336,14 @@ function get3TopItems(arr) { * [ 1, '2' ] => 1 */ function getPositivesCount(arr) { - throw new Error('Not implemented'); + let count=0 + arr.map(elem=>{ + if (typeof elem ==="number"){ + if (elem>0) count++ + } + return elem + }) + return count } /** @@ -321,7 +360,10 @@ function getPositivesCount(arr) { * [ 'one','one','one','zero' ] => [ 'zero','one','one','one' ] */ function sortDigitNamesByNumericOrder(arr) { - throw new Error('Not implemented'); + let Newarr=[{"name": "zero","number":0},{"name": "one","number":1}, {"name": "two","number":2}, {"name": "three","number":3}, {"name": "four","number":4}, {"name": "five","number":5}, {"name": "six","number":6}, {"name": "seven","number":7}, {"name": "eight","number":8}, {"name": "nine","number":9}] + return arr.sort((a,b)=>{ + return (Newarr.find(x => x.name === a).number-Newarr.find(x => x.name === b).number) + }) } /** @@ -337,7 +379,9 @@ function sortDigitNamesByNumericOrder(arr) { * [ 1, 10, 100, 1000 ] => 1111 */ function getItemsSum(arr) { - throw new Error('Not implemented'); + return arr.reduce((sum, cur)=>{ + return sum+cur + },0) } /** @@ -353,7 +397,10 @@ function getItemsSum(arr) { * [ null, undefined, NaN, false, 0, '' ] => 6 */ function getFalsyValuesCount(arr) { - throw new Error('Not implemented'); + return arr.reduce((sum, cur)=>{ + if (!cur) return sum+=1 + else return sum + },0) } /** @@ -371,7 +418,10 @@ function getFalsyValuesCount(arr) { * [ true, 0, 1, 'true' ], true => 1 */ function findAllOccurences(arr, item) { - throw new Error('Not implemented'); + return arr.reduce((sum, cur)=>{ + if (cur===item) return sum+=1 + else return sum + },0) } /** @@ -386,7 +436,7 @@ function findAllOccurences(arr, item) { * ['rock', 'paper', 'scissors'] => 'rock,paper,scissors' */ function toStringList(arr) { - throw new Error('Not implemented'); + return arr.join(",") } @@ -415,7 +465,11 @@ function toStringList(arr) { * { country: 'Russia', city: 'Saint Petersburg' } */ function sortCitiesArray(arr) { - throw new Error('Not implemented'); + return arr.sort((a,b)=>{ + if(a.country < b.country) { return -1; } + if(a.country > b.country) { return 1; } + if(a.city < b.city) { return -1; } + }) } /** @@ -437,7 +491,15 @@ function sortCitiesArray(arr) { * [0,0,0,0,1]] */ function getIdentityMatrix(n) { - throw new Error('Not implemented'); + let arr=[], count=0 + while (count [ 3 ] */ function getIntervalArray(start, end) { - throw new Error('Not implemented'); + let newArr=[] + while (end>=start){ + newArr.push(start) + start+=1 + } + return newArr } /** @@ -469,9 +536,15 @@ function getIntervalArray(start, end) { * [ 1, 1, 2, 2, 3, 3, 4, 4] => [ 1, 2, 3, 4] */ function distinct(arr) { - throw new Error('Not implemented'); + let newArr = []; + arr.reduce((sum, elem) => { + if (!newArr.includes(elem)) { + newArr.push(elem); + }},0) + return newArr; } + /** * Groups elements of the specified array by key. * Returns multimap of keys extracted from array elements via keySelector callback @@ -503,7 +576,20 @@ function distinct(arr) { * } */ function group(array, keySelector, valueSelector) { - throw new Error('Not implemented'); + const map = new Map(); + array.map((item) => { + const key = keySelector(item); + const value = valueSelector(item) + const collection = map.get(key); + if (!collection) { + map.set(key, [value]); + } else { + collection.push(value); + } + }); + return map; + + //throw new Error('Not implemented'); } @@ -519,7 +605,7 @@ function group(array, keySelector, valueSelector) { * ['one','two','three'], x=>x.split('') => ['o','n','e','t','w','o','t','h','r','e','e'] */ function selectMany(arr, childrenSelector) { - throw new Error('Not implemented'); + return arr.flatMap(childrenSelector) } @@ -536,7 +622,8 @@ function selectMany(arr, childrenSelector) { * [[[ 1, 2, 3]]], [ 0, 0, 1 ] => 2 (arr[0][0][1]) */ function getElementByIndexes(arr, indexes) { - throw new Error('Not implemented'); + let place=indexes.map(elem=>{return `[${elem}]`}).join("") + return eval(`arr${place}`) } @@ -559,7 +646,21 @@ function getElementByIndexes(arr, indexes) { * */ function swapHeadAndTail(arr) { - throw new Error('Not implemented'); + const point = arr.length + if (point===1) return arr + if (point%2!=0){ + let newArr=[] + newArr=newArr.concat(arr.slice(Math.round(point/2))) + newArr.push(arr[Math.floor(point/2)]) + newArr=newArr.concat(arr.slice(0, Math.floor(point/2))) + return newArr + } + else { + let newArr=[] + newArr= newArr.concat(arr.slice(point/2)) + newArr=newArr.concat(arr.slice(0, point/2)) + return newArr + } } From 0381434907d8abdf966a5c81898669ae1dc14f2d Mon Sep 17 00:00:00 2001 From: tapeghad Date: Fri, 3 Jul 2020 18:23:34 +0300 Subject: [PATCH 18/30] task_1_4 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 008cbc167..a9c0a7fc0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ language: node_js node_js: - - "5.10.0" + - "12.16.1" From ecb2d049fa39a304865d2efa92abc8ae45878515 Mon Sep 17 00:00:00 2001 From: tapeghad Date: Fri, 3 Jul 2020 18:28:07 +0300 Subject: [PATCH 19/30] task_1_4 --- task/02-numbers-tasks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task/02-numbers-tasks.js b/task/02-numbers-tasks.js index 508c0b846..4da68bdbc 100644 --- a/task/02-numbers-tasks.js +++ b/task/02-numbers-tasks.js @@ -1,6 +1,6 @@ 'use strict'; -const { interfaces } = require("mocha"); + /******************************************************************************************** * * From 5c58e890637dfb508fcd9670bc6afdc1e2f70de0 Mon Sep 17 00:00:00 2001 From: tapeghad Date: Fri, 3 Jul 2020 18:40:30 +0300 Subject: [PATCH 20/30] task_1_4 --- task/03-date-tasks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task/03-date-tasks.js b/task/03-date-tasks.js index dce30e3fe..3e2253157 100644 --- a/task/03-date-tasks.js +++ b/task/03-date-tasks.js @@ -110,7 +110,7 @@ function timeSpanToString(startDate, endDate) { * Date.UTC(2016,3,5,21, 0) => Math.PI/2 */ function angleBetweenClockHands(date) { - let total = date.getHours()-3>12? 0.5*(60*(date.getHours()-15)-11*date.getMinutes()):0.5*(60*(date.getHours()-3)-11*date.getMinutes()) + let total = date.getHours()>12? 0.5*(60*(date.getHours()-12)-11*date.getMinutes()):0.5*(60*(date.getHours())-11*date.getMinutes()) return Math.abs(total)>180 ? (360-Math.abs(total))*Math.PI/180:Math.abs(total)*Math.PI/180 } From ad88a75f0465d61fccba4e58608b6d1102cfec59 Mon Sep 17 00:00:00 2001 From: tapeghad Date: Fri, 10 Jul 2020 22:07:53 +0300 Subject: [PATCH 21/30] task_1_4 ' --- task/01-strings-tasks.js | 50 +++++------ task/02-numbers-tasks.js | 35 ++++---- task/03-date-tasks.js | 28 +++--- task/04-arrays-tasks.js | 184 +++++++++++++++++++-------------------- 4 files changed, 143 insertions(+), 154 deletions(-) diff --git a/task/01-strings-tasks.js b/task/01-strings-tasks.js index 4a0263784..ef7ec143d 100644 --- a/task/01-strings-tasks.js +++ b/task/01-strings-tasks.js @@ -22,7 +22,7 @@ * '', 'bb' => 'bb' */ function concatenateStrings(value1, value2) { - return value1+value2 + return value1+value2; } @@ -38,7 +38,7 @@ function concatenateStrings(value1, value2) { * '' => 0 */ function getStringLength(value) { - return value.length + return value.length; } /** @@ -55,7 +55,7 @@ function getStringLength(value) { * 'Chuck','Norris' => 'Hello, Chuck Norris!' */ function getStringFromTemplate(firstName, lastName) { - return `Hello, ${firstName} ${lastName}!` + return `Hello, ${firstName} ${lastName}!`; } /** @@ -69,8 +69,8 @@ function getStringFromTemplate(firstName, lastName) { * 'Hello, Chuck Norris!' => 'Chuck Norris' */ function extractNameFromTemplate(value) { - let temp = value.slice(7) - return temp.slice(0,-1) + let name = value.slice(7); + return name.slice(0,-1); } @@ -85,7 +85,7 @@ function extractNameFromTemplate(value) { * 'cat' => 'c' */ function getFirstChar(value) { - return value[0] + return value[0]; } /** @@ -100,7 +100,7 @@ function getFirstChar(value) { * '\tHello, World! ' => 'Hello, World!' */ function removeLeadingAndTrailingWhitespaces(value) { - return value.trim(/\t| |/g,'') + return value.trim(/\t| |/g,''); } /** @@ -115,7 +115,7 @@ function removeLeadingAndTrailingWhitespaces(value) { * 'cat', 3 => 'catcatcat' */ function repeatString(value, count) { - return value.repeat(count) + return value.repeat(count); } /** @@ -131,7 +131,7 @@ function repeatString(value, count) { * 'ABABAB','BA' => 'ABAB' */ function removeFirstOccurrences(str, value) { - return str.replace(value, '') + return str.replace(value, ''); } /** @@ -146,7 +146,7 @@ function removeFirstOccurrences(str, value) { * '' => 'a' */ function unbracketTag(str) { - return str.replace(/<|>|/g,'') + return str.replace(/<|>|/g,''); } @@ -161,7 +161,7 @@ function unbracketTag(str) { * 'abcdefghijklmnopqrstuvwxyz' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' */ function convertToUpperCase(str) { - return str.toUpperCase() + return str.toUpperCase(); } /** @@ -175,7 +175,7 @@ function convertToUpperCase(str) { * 'info@gmail.com' => ['info@gmail.com'] */ function extractEmails(str) { - return str.split(";") + return str.split(";"); } /** @@ -202,14 +202,14 @@ function extractEmails(str) { * */ function getRectangleString(width, height) { - let str="┌"+"─".repeat(width-2)+"┐"+'\n' - let count=height-2 + let str="┌"+"─".repeat(width-2)+"┐"+'\n'; + let count=height-2; while (count>0) { str = str.concat("│"+" ".repeat(width-2))+"│\n" count-- } - return str.concat("└"+"─".repeat(width-2)+"┘\n") + return str.concat("└"+"─".repeat(width-2)+"┘\n"); } @@ -229,18 +229,18 @@ function getRectangleString(width, height) { * */ function encodeToRot13(str) { - let arr=[], count=0 + let arr=[], count=0; while (count=78 & str.charCodeAt(count) <=90)| (str.charCodeAt(count) >=110 & str.charCodeAt(count)<=122)) - arr.push(str.charCodeAt(count)-13) + arr.push(str.charCodeAt(count)-13); else if ((str.charCodeAt(count) >=65 & str.charCodeAt(count) <=77)| (str.charCodeAt(count) >=97 & str.charCodeAt(count)<=109)) - arr.push(str.charCodeAt(count)+13) - else arr.push(str.charCodeAt(count)) - count++ + arr.push(str.charCodeAt(count)+13); + else arr.push(str.charCodeAt(count)); + count++; } - return String.fromCharCode(...arr) + return String.fromCharCode(...arr); } /** @@ -257,8 +257,8 @@ function encodeToRot13(str) { * isString(new String('test')) => true */ function isString(value) { - if (typeof value ==="string" || value instanceof String) return true - else return false + if (typeof value ==="string" || value instanceof String) return true; + else return false; } @@ -290,8 +290,8 @@ function getCardId(value) { let arr=['A♣','2♣','3♣','4♣','5♣','6♣','7♣','8♣','9♣','10♣','J♣','Q♣','K♣', 'A♦','2♦','3♦','4♦','5♦','6♦','7♦','8♦','9♦','10♦','J♦','Q♦','K♦', 'A♥','2♥','3♥','4♥','5♥','6♥','7♥','8♥','9♥','10♥','J♥','Q♥','K♥', - 'A♠','2♠','3♠','4♠','5♠','6♠','7♠','8♠','9♠','10♠','J♠','Q♠','K♠'] - return arr.indexOf(value) + 'A♠','2♠','3♠','4♠','5♠','6♠','7♠','8♠','9♠','10♠','J♠','Q♠','K♠']; + return arr.indexOf(value); } diff --git a/task/02-numbers-tasks.js b/task/02-numbers-tasks.js index 4da68bdbc..d96519649 100644 --- a/task/02-numbers-tasks.js +++ b/task/02-numbers-tasks.js @@ -24,7 +24,7 @@ * 5, 5 => 25 */ function getRectangleArea(width, height) { - return width*height + return width*height; } @@ -40,7 +40,7 @@ function getRectangleArea(width, height) { * 0 => 0 */ function getCicleCircumference(radius) { - return 2*radius*Math.PI + return 2*radius*Math.PI; } /** @@ -56,8 +56,7 @@ function getCicleCircumference(radius) { * -3, 3 => 0 */ function getAverage(value1, value2) { - return value1/2 +value2/2 - //throw new Error('Not implemented'); + return value1/2 +value2/2; } /** @@ -76,7 +75,7 @@ function getAverage(value1, value2) { * (-5,0) (10,-10) => 18.027756377319946 */ function getDistanceBetweenPoints(x1, y1, x2, y2) { - return Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2)) + return Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2)); } /** @@ -92,7 +91,7 @@ function getDistanceBetweenPoints(x1, y1, x2, y2) { * 5*x = 0 => 0 */ function getLinearEquationRoot(a, b) { - return -b/a + return -b/a; } @@ -114,7 +113,7 @@ function getLinearEquationRoot(a, b) { * (0,1) (1,2) => 0 */ function getAngleBetweenVectors(x1, y1, x2, y2) { - return Math.acos((x1*x2+y1*y2)/(Math.sqrt(Math.pow(x1,2)+Math.pow(y1,2)) * Math.sqrt(Math.sqrt(Math.pow(x2,2)+Math.pow(y2,2))))) + return Math.acos((x1*x2+y1*y2)/(Math.sqrt(Math.pow(x1,2)+Math.pow(y1,2)) * Math.sqrt(Math.sqrt(Math.pow(x2,2)+Math.pow(y2,2))))); } /** @@ -130,7 +129,7 @@ function getAngleBetweenVectors(x1, y1, x2, y2) { * 0 => 0 */ function getLastDigit(value) { - return value%10 + return value%10; } @@ -146,7 +145,7 @@ function getLastDigit(value) { * '-525.5' => -525.5 */ function parseNumberFromString(value) { - return parseFloat(value) + return parseFloat(value); } /** @@ -163,7 +162,7 @@ function parseNumberFromString(value) { * 1,2,3 => 3.741657386773941 */ function getParallelipidedDiagonal(a,b,c) { - return Math.sqrt(Math.pow(a,2) +Math.pow(b,2)+Math.pow(c,2)) + return Math.sqrt(Math.pow(a,2) +Math.pow(b,2)+Math.pow(c,2)); } /** @@ -184,7 +183,7 @@ function getParallelipidedDiagonal(a,b,c) { * 1678, 3 => 2000 */ function roundToPowerOfTen(num, pow) { - return Math.round(num/10**pow)*10**pow + return Math.round(num/10**pow)*10**pow; } @@ -206,16 +205,16 @@ function roundToPowerOfTen(num, pow) { * 17 => true */ function isPrime(n) { - let check=true, count=2 + let check=true, count=2; while (count <=n/2){ if (n%count===0) {check = false; - break + break; } - count++ + count++; } - if (check===true) return true - else return false + if (check===true) return true; + else return false; } /** @@ -234,8 +233,8 @@ function isPrime(n) { * toNumber(new Number(42), 0) => 42 */ function toNumber(value, def) { - if (parseInt(value)) return parseInt(value) - return def + if (parseInt(value)) return parseInt(value); + return def; //throw new Error('Not implemented'); } diff --git a/task/03-date-tasks.js b/task/03-date-tasks.js index 3e2253157..b959671ea 100644 --- a/task/03-date-tasks.js +++ b/task/03-date-tasks.js @@ -79,20 +79,18 @@ function isLeapYear(date) { * Date(2000,1,1,10,0,0), Date(2000,1,1,15,20,10,453) => "05:20:10.453" */ function timeSpanToString(startDate, endDate) { - let time_p=endDate-startDate + let time_period=endDate-startDate; let milliseconds = parseInt((time_p % 1000)), - seconds = Math.floor((time_p / 1000) % 60), - minutes = Math.floor((time_p / (1000 * 60)) % 60), - hours = Math.floor((time_p / (1000 * 60 * 60)) % 24); - hours = (startDate.getDay()!== endDate.getDay()) ? hours+24 : hours; - hours = (hours < 10) ? "0" + hours : hours; - minutes = (minutes < 10) ? "0" + minutes : minutes; - seconds = (seconds < 10) ? "0" + seconds : seconds; - milliseconds = (milliseconds < 100) ? "0" + milliseconds : milliseconds; - milliseconds = (milliseconds < 10) ? "0" + milliseconds : milliseconds; - - return hours + ":" + minutes + ":" + seconds + "." + milliseconds -//throw new Error('Not implemented'); + seconds = Math.floor((time_period / 1000) % 60), + minutes = Math.floor((time_period / (1000 * 60)) % 60), + hours = Math.floor((time_period / (1000 * 60 * 60)) % 24); + hours = (startDate.getDay()!== endDate.getDay()) ? hours+24 : hours; + hours = (hours < 10) ? "0" + hours : hours; + minutes = (minutes < 10) ? "0" + minutes : minutes; + seconds = (seconds < 10) ? "0" + seconds : seconds; + milliseconds = (milliseconds < 100) ? "0" + milliseconds : milliseconds; + milliseconds = (milliseconds < 10) ? "0" + milliseconds : milliseconds; + return hours + ":" + minutes + ":" + seconds + "." + milliseconds; } @@ -110,8 +108,8 @@ function timeSpanToString(startDate, endDate) { * Date.UTC(2016,3,5,21, 0) => Math.PI/2 */ function angleBetweenClockHands(date) { - let total = date.getHours()>12? 0.5*(60*(date.getHours()-12)-11*date.getMinutes()):0.5*(60*(date.getHours())-11*date.getMinutes()) - return Math.abs(total)>180 ? (360-Math.abs(total))*Math.PI/180:Math.abs(total)*Math.PI/180 + let time = date.getHours()>12? 0.5*(60*(date.getHours()-12)-11*date.getMinutes()):0.5*(60*(date.getHours())-11*date.getMinutes()); + return Math.abs(time)>180 ? (360-Math.abs(time))*Math.PI/180:Math.abs(time)*Math.PI/180; } diff --git a/task/04-arrays-tasks.js b/task/04-arrays-tasks.js index e2e4c3a8b..10f6f9eda 100644 --- a/task/04-arrays-tasks.js +++ b/task/04-arrays-tasks.js @@ -23,8 +23,8 @@ * [0, 1, 2, 3, 4, 5], 5 => 5 */ function findElement(arr, value) { - if (arr.includes(value)) return arr.indexOf(value) - else return -1 + if (arr.includes(value)) return arr.indexOf(value); + else return -1; } /** @@ -39,13 +39,11 @@ function findElement(arr, value) { * 5 => [ 1, 3, 5, 7, 9 ] */ function generateOdds(len) { - let arr=[], count=1 - while (len>0){ - arr.push(count); + let arr=new Array(len), count = 1; + return arr.map(elem=>{ + elem=count; count+=2 - len-- - } - return arr + }) } @@ -61,7 +59,7 @@ function generateOdds(len) { * [] => [] */ function doubleArray(arr) { - return arr.concat(arr) + return arr.concat(arr); } @@ -78,8 +76,8 @@ function doubleArray(arr) { */ function getArrayOfPositives(arr) { return arr.filter(elem =>{ - return elem>0 - }) + return elem>0; + }); } /** @@ -95,8 +93,8 @@ function getArrayOfPositives(arr) { */ function getArrayOfStrings(arr) { return arr.filter(elem =>{ - return typeof elem ==="string" - }) + return typeof elem ==="string"; + }); } /** @@ -114,8 +112,8 @@ function getArrayOfStrings(arr) { */ function removeFalsyValues(arr) { return arr.filter(elem =>{ - if (elem) return elem - }) + if (elem) return elem; + }); } /** @@ -130,8 +128,8 @@ function removeFalsyValues(arr) { */ function getUpperCaseStrings(arr) { return arr.map(elem =>{ - return elem.toUpperCase() - }) + return elem.toUpperCase(); + }); } @@ -147,8 +145,8 @@ function getUpperCaseStrings(arr) { */ function getStringsLength(arr) { return arr.map(elem =>{ - return elem.length - }) + return elem.length; + }); } /** @@ -163,7 +161,7 @@ function getStringsLength(arr) { * [ 1, 'b', 'c'], 0, 'x' => [ 'x', 1, 'b', 'c' ] */ function insertItem(arr, item, index) { - return arr.splice(index, 0, item) + return arr.splice(index, 0, item); } /** @@ -177,7 +175,7 @@ function insertItem(arr, item, index) { * [ 'a', 'b', 'c', 'd'], 3 => [ 'a', 'b', 'c' ] */ function getHead(arr, n) { - return arr.slice(0, n) + return arr.slice(0, n); } @@ -192,7 +190,7 @@ function getHead(arr, n) { * [ 'a', 'b', 'c', 'd'], 3 => [ 'b', 'c', 'd' ] */ function getTail(arr, n) { - return arr.slice(-n) + return arr.slice(-n); } @@ -217,7 +215,7 @@ function getTail(arr, n) { * +'30,31,32,33,34' */ function toCsvText(arr) { - return arr.join("\n") + return arr.join("\n"); } /** @@ -232,7 +230,7 @@ function toCsvText(arr) { * [ 10, 100, -1 ] => [ 100, 10000, 1 ] */ function toArrayOfSquares(arr) { - return arr.map(elem=>{return Math.pow(elem, 2)}) + return arr.map(elem=>{return Math.pow(elem, 2)}); } @@ -251,12 +249,12 @@ function toArrayOfSquares(arr) { * [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] => [ 1, 3, 6, 10, 15, 21, 28, 36, 45, 55 ] */ function getMovingSum(arr) { - let newArr=[] + let newArr=[]; arr.reduce(function(sum, current) { newArr.push(sum+current); return sum + current; },0); - return newArr + return newArr; } /** @@ -272,8 +270,8 @@ function getMovingSum(arr) { */ function getSecondItems(arr) { return arr.filter(elem=>{ - return arr.indexOf(elem)%2===1 - }) + return arr.indexOf(elem)%2===1; + }); } @@ -292,15 +290,13 @@ function getSecondItems(arr) { * [ 1,2,3,4,5 ] => [ 1, 2,2, 3,3,3, 4,4,4,4, 5,5,5,5,5 ] */ function propagateItemsByPositionIndex(arr) { - let newArr=[] + let newArr=[]; arr.map(elem=>{ - let count=arr.indexOf(elem)+1 - while (count>0){ - newArr.push(elem) - count-- - } - }) - return newArr + let position=arr.indexOf(elem)+1; + let help_arr = new Array(position).fill(elem) + newArr = newArr.concat(help_arr) + }); + return newArr; } @@ -336,14 +332,14 @@ function get3TopItems(arr) { * [ 1, '2' ] => 1 */ function getPositivesCount(arr) { - let count=0 + let count_positive=0; arr.map(elem=>{ if (typeof elem ==="number"){ - if (elem>0) count++ + if (elem>0) count_positive++; } - return elem + return elem; }) - return count + return count_positive; } /** @@ -360,10 +356,10 @@ function getPositivesCount(arr) { * [ 'one','one','one','zero' ] => [ 'zero','one','one','one' ] */ function sortDigitNamesByNumericOrder(arr) { - let Newarr=[{"name": "zero","number":0},{"name": "one","number":1}, {"name": "two","number":2}, {"name": "three","number":3}, {"name": "four","number":4}, {"name": "five","number":5}, {"name": "six","number":6}, {"name": "seven","number":7}, {"name": "eight","number":8}, {"name": "nine","number":9}] + let Newarr=[{"name": "zero","number":0},{"name": "one","number":1}, {"name": "two","number":2}, {"name": "three","number":3}, {"name": "four","number":4}, {"name": "five","number":5}, {"name": "six","number":6}, {"name": "seven","number":7}, {"name": "eight","number":8}, {"name": "nine","number":9}]; return arr.sort((a,b)=>{ - return (Newarr.find(x => x.name === a).number-Newarr.find(x => x.name === b).number) - }) + return (Newarr.find(x => x.name === a).number-Newarr.find(x => x.name === b).number); + }); } /** @@ -381,8 +377,8 @@ function sortDigitNamesByNumericOrder(arr) { function getItemsSum(arr) { return arr.reduce((sum, cur)=>{ return sum+cur - },0) -} + },0); +}; /** * Returns the number of all falsy value in the specified array @@ -400,8 +396,8 @@ function getFalsyValuesCount(arr) { return arr.reduce((sum, cur)=>{ if (!cur) return sum+=1 else return sum - },0) -} + },0); +}; /** * Returns a number of all occurences of the specified item in an array @@ -421,8 +417,8 @@ function findAllOccurences(arr, item) { return arr.reduce((sum, cur)=>{ if (cur===item) return sum+=1 else return sum - },0) -} + },0); +}; /** * Concatenates all elements from specified array into single string with ',' delimeter @@ -436,7 +432,7 @@ function findAllOccurences(arr, item) { * ['rock', 'paper', 'scissors'] => 'rock,paper,scissors' */ function toStringList(arr) { - return arr.join(",") + return arr.join(","); } @@ -469,7 +465,7 @@ function sortCitiesArray(arr) { if(a.country < b.country) { return -1; } if(a.country > b.country) { return 1; } if(a.city < b.city) { return -1; } - }) + }); } /** @@ -491,15 +487,15 @@ function sortCitiesArray(arr) { * [0,0,0,0,1]] */ function getIdentityMatrix(n) { - let arr=[], count=0 - while (count{ + let newArr=new Array(n); + newArr.fill(0); + newArr[count_row]=1; + elem = newArr; + count_row++; + }); + return matrix; } /** @@ -516,12 +512,11 @@ function getIdentityMatrix(n) { * 3, 3 => [ 3 ] */ function getIntervalArray(start, end) { - let newArr=[] - while (end>=start){ - newArr.push(start) - start+=1 - } - return newArr + let newArr=new Array(end - start +1), count=start-1; + return newArr.map(elem=>{ + count++; + return count + }); } /** @@ -577,19 +572,16 @@ function distinct(arr) { */ function group(array, keySelector, valueSelector) { const map = new Map(); - array.map((item) => { - const key = keySelector(item); - const value = valueSelector(item) - const collection = map.get(key); - if (!collection) { - map.set(key, [value]); - } else { - collection.push(value); - } - }); - return map; - - //throw new Error('Not implemented'); + array.map((item) => { + const key = keySelector(item); + const value = valueSelector(item); + const collection = map.get(key); + if (!collection) { + map.set(key, [value]); + } else { + collection.push(value); + }}); + return map; } @@ -605,7 +597,7 @@ function group(array, keySelector, valueSelector) { * ['one','two','three'], x=>x.split('') => ['o','n','e','t','w','o','t','h','r','e','e'] */ function selectMany(arr, childrenSelector) { - return arr.flatMap(childrenSelector) + return arr.flatMap(childrenSelector); } @@ -622,8 +614,8 @@ function selectMany(arr, childrenSelector) { * [[[ 1, 2, 3]]], [ 0, 0, 1 ] => 2 (arr[0][0][1]) */ function getElementByIndexes(arr, indexes) { - let place=indexes.map(elem=>{return `[${elem}]`}).join("") - return eval(`arr${place}`) + let place=indexes.map(elem=>{return `[${elem}]`}).join(""); + return eval(`arr${place}`); } @@ -646,20 +638,20 @@ function getElementByIndexes(arr, indexes) { * */ function swapHeadAndTail(arr) { - const point = arr.length - if (point===1) return arr - if (point%2!=0){ - let newArr=[] - newArr=newArr.concat(arr.slice(Math.round(point/2))) - newArr.push(arr[Math.floor(point/2)]) - newArr=newArr.concat(arr.slice(0, Math.floor(point/2))) - return newArr - } - else { - let newArr=[] - newArr= newArr.concat(arr.slice(point/2)) - newArr=newArr.concat(arr.slice(0, point/2)) - return newArr + const arr_length = arr.length; + if (arr_length===1) return arr; + if (arr_length%2!=0){ + let newArr=[]; + newArr=newArr.concat(arr.slice(Math.round(arr_length/2))); + newArr.push(arr[Math.floor(arr_length/2)]); + newArr=newArr.concat(arr.slice(0, Math.floor(arr_length/2))); + return newArr; + } + else { + let newArr=[]; + newArr= newArr.concat(arr.slice(arr_length/2)); + newArr=newArr.concat(arr.slice(0, arr_length/2)); + return newArr; } } From 97e0327d86dbb4d2c007d7a0946fdeccf46f403e Mon Sep 17 00:00:00 2001 From: tapeghad Date: Fri, 10 Jul 2020 22:36:07 +0300 Subject: [PATCH 22/30] task_1_4 ' --- task/03-date-tasks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task/03-date-tasks.js b/task/03-date-tasks.js index b959671ea..415ec60e7 100644 --- a/task/03-date-tasks.js +++ b/task/03-date-tasks.js @@ -80,7 +80,7 @@ function isLeapYear(date) { */ function timeSpanToString(startDate, endDate) { let time_period=endDate-startDate; - let milliseconds = parseInt((time_p % 1000)), + let milliseconds = parseInt((time_period % 1000)), seconds = Math.floor((time_period / 1000) % 60), minutes = Math.floor((time_period / (1000 * 60)) % 60), hours = Math.floor((time_period / (1000 * 60 * 60)) % 24); From 9b0af16c437ab374d3c97b61cd32621b07c96b1b Mon Sep 17 00:00:00 2001 From: tapeghad Date: Fri, 10 Jul 2020 22:47:29 +0300 Subject: [PATCH 23/30] task_1_4 ' --- task/04-arrays-tasks.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/task/04-arrays-tasks.js b/task/04-arrays-tasks.js index 10f6f9eda..50e64eb1c 100644 --- a/task/04-arrays-tasks.js +++ b/task/04-arrays-tasks.js @@ -487,12 +487,11 @@ function sortCitiesArray(arr) { * [0,0,0,0,1]] */ function getIdentityMatrix(n) { - let matrix= new Array(n), count_row=0; + let matrix= new Array(n).fill(undefined), count_row=0; matrix.map(elem=>{ - let newArr=new Array(n); - newArr.fill(0); + let newArr=new Array(n).fill(0); newArr[count_row]=1; - elem = newArr; + matrix[count_row]=newArr; count_row++; }); return matrix; @@ -512,7 +511,7 @@ function getIdentityMatrix(n) { * 3, 3 => [ 3 ] */ function getIntervalArray(start, end) { - let newArr=new Array(end - start +1), count=start-1; + let newArr=new Array(end - start +1).fill(undefined), count=start-1; return newArr.map(elem=>{ count++; return count From 8c38905733ea86b668e5d80b9adf842cdb39e733 Mon Sep 17 00:00:00 2001 From: tapeghad Date: Fri, 10 Jul 2020 23:20:19 +0300 Subject: [PATCH 24/30] task_1_4 ' --- task/03-date-tasks.js | 4 ++-- task/04-arrays-tasks.js | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/task/03-date-tasks.js b/task/03-date-tasks.js index 415ec60e7..85d700974 100644 --- a/task/03-date-tasks.js +++ b/task/03-date-tasks.js @@ -108,8 +108,8 @@ function timeSpanToString(startDate, endDate) { * Date.UTC(2016,3,5,21, 0) => Math.PI/2 */ function angleBetweenClockHands(date) { - let time = date.getHours()>12? 0.5*(60*(date.getHours()-12)-11*date.getMinutes()):0.5*(60*(date.getHours())-11*date.getMinutes()); - return Math.abs(time)>180 ? (360-Math.abs(time))*Math.PI/180:Math.abs(time)*Math.PI/180; + let angle_time = Math.abs((date.getHours() * 60 + date.getMinutes() + date.getTimezoneOffset() - date.getMinutes() * 12) / 2) % 360; + return (angle_time <= 180 ? angle_time : 360 - angle_time) / 180 * Math.PI; } diff --git a/task/04-arrays-tasks.js b/task/04-arrays-tasks.js index 50e64eb1c..67ec55471 100644 --- a/task/04-arrays-tasks.js +++ b/task/04-arrays-tasks.js @@ -39,10 +39,10 @@ function findElement(arr, value) { * 5 => [ 1, 3, 5, 7, 9 ] */ function generateOdds(len) { - let arr=new Array(len), count = 1; + let arr=new Array(len).fill(undefined), count = -1; return arr.map(elem=>{ - elem=count; - count+=2 + count+=2; + return count; }) } @@ -488,11 +488,11 @@ function sortCitiesArray(arr) { */ function getIdentityMatrix(n) { let matrix= new Array(n).fill(undefined), count_row=0; - matrix.map(elem=>{ + matrix = matrix.map(elem=>{ let newArr=new Array(n).fill(0); newArr[count_row]=1; - matrix[count_row]=newArr; count_row++; + return newArr; }); return matrix; } From e46fe00b3a2fe0982892b2483d1905946ab0fb65 Mon Sep 17 00:00:00 2001 From: tapeghad Date: Fri, 10 Jul 2020 23:21:39 +0300 Subject: [PATCH 25/30] task_1_4 ' --- task/03-date-tasks.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/task/03-date-tasks.js b/task/03-date-tasks.js index 85d700974..7c80bcd55 100644 --- a/task/03-date-tasks.js +++ b/task/03-date-tasks.js @@ -108,8 +108,8 @@ function timeSpanToString(startDate, endDate) { * Date.UTC(2016,3,5,21, 0) => Math.PI/2 */ function angleBetweenClockHands(date) { - let angle_time = Math.abs((date.getHours() * 60 + date.getMinutes() + date.getTimezoneOffset() - date.getMinutes() * 12) / 2) % 360; - return (angle_time <= 180 ? angle_time : 360 - angle_time) / 180 * Math.PI; + let angle_time=Math.abs((date.getHours()*60+date.getMinutes()+date.getTimezoneOffset()-date.getMinutes()*12)/2)%360; + return (angle_time<=180?angle_time:360-angle_time)/180*Math.PI; } From 33553bd72070915a418bb59bb2b22b418bab566f Mon Sep 17 00:00:00 2001 From: tapeghad Date: Sat, 11 Jul 2020 18:04:38 +0300 Subject: [PATCH 26/30] task_1_4 --- task/01-strings-tasks.js | 17 ++++++++++------- task/02-numbers-tasks.js | 14 ++++++-------- task/03-date-tasks.js | 19 +++++++++---------- task/04-arrays-tasks.js | 16 ++++++++-------- 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/task/01-strings-tasks.js b/task/01-strings-tasks.js index ef7ec143d..e31462797 100644 --- a/task/01-strings-tasks.js +++ b/task/01-strings-tasks.js @@ -203,12 +203,12 @@ function extractEmails(str) { */ function getRectangleString(width, height) { let str="┌"+"─".repeat(width-2)+"┐"+'\n'; - let count=height-2; - while (count>0) + let count_height=height-2; + while (count_height>0) { - str = str.concat("│"+" ".repeat(width-2))+"│\n" - count-- - } + str = str.concat("│"+" ".repeat(width-2))+"│\n"; + count_height--; + }; return str.concat("└"+"─".repeat(width-2)+"┘\n"); } @@ -233,13 +233,16 @@ function encodeToRot13(str) { while (count=78 & str.charCodeAt(count) <=90)| (str.charCodeAt(count) >=110 & str.charCodeAt(count)<=122)) + { arr.push(str.charCodeAt(count)-13); + } else if ((str.charCodeAt(count) >=65 & str.charCodeAt(count) <=77)| (str.charCodeAt(count) >=97 & str.charCodeAt(count)<=109)) + { arr.push(str.charCodeAt(count)+13); + } else arr.push(str.charCodeAt(count)); count++; - - } + }; return String.fromCharCode(...arr); } diff --git a/task/02-numbers-tasks.js b/task/02-numbers-tasks.js index d96519649..95bee57d7 100644 --- a/task/02-numbers-tasks.js +++ b/task/02-numbers-tasks.js @@ -205,16 +205,16 @@ function roundToPowerOfTen(num, pow) { * 17 => true */ function isPrime(n) { - let check=true, count=2; - while (count <=n/2){ - if (n%count===0) + let check=true, count=2; + while (count <=n/2){ + if (n%count===0) {check = false; break; } count++; - } - if (check===true) return true; - else return false; + } + if (check===true) return true; + else return false; } /** @@ -235,8 +235,6 @@ function isPrime(n) { function toNumber(value, def) { if (parseInt(value)) return parseInt(value); return def; - //throw new Error('Not implemented'); - } module.exports = { diff --git a/task/03-date-tasks.js b/task/03-date-tasks.js index 7c80bcd55..9d38a83d6 100644 --- a/task/03-date-tasks.js +++ b/task/03-date-tasks.js @@ -22,7 +22,7 @@ * 'Sun, 17 May 1998 03:00:00 GMT+01' => Date() */ function parseDataFromRfc2822(value) { - return Date.parse(value) + return Date.parse(value); } /** @@ -37,7 +37,7 @@ function parseDataFromRfc2822(value) { * '2016-01-19T08:07:37Z' => Date() */ function parseDataFromIso8601(value) { - return Date.parse(value) + return Date.parse(value); } @@ -56,10 +56,10 @@ function parseDataFromIso8601(value) { * Date(2015,1,1) => false */ function isLeapYear(date) { - if (date.getFullYear()%4!=0) return false - else if (date.getFullYear()%100!=0) return true - else if (date.getFullYear()%400!=0) return false - else return true + if (date.getFullYear()%4!=0) return false; + else if (date.getFullYear()%100!=0) return true; + else if (date.getFullYear()%400!=0) return false; + else return true; } @@ -80,10 +80,9 @@ function isLeapYear(date) { */ function timeSpanToString(startDate, endDate) { let time_period=endDate-startDate; - let milliseconds = parseInt((time_period % 1000)), - seconds = Math.floor((time_period / 1000) % 60), - minutes = Math.floor((time_period / (1000 * 60)) % 60), - hours = Math.floor((time_period / (1000 * 60 * 60)) % 24); + let milliseconds = parseInt((time_period % 1000)), seconds = Math.floor((time_period / 1000) % 60), + minutes = Math.floor((time_period / (1000 * 60)) % 60), + hours = Math.floor((time_period / (1000 * 60 * 60)) % 24); hours = (startDate.getDay()!== endDate.getDay()) ? hours+24 : hours; hours = (hours < 10) ? "0" + hours : hours; minutes = (minutes < 10) ? "0" + minutes : minutes; diff --git a/task/04-arrays-tasks.js b/task/04-arrays-tasks.js index 67ec55471..6d533a3bb 100644 --- a/task/04-arrays-tasks.js +++ b/task/04-arrays-tasks.js @@ -43,7 +43,7 @@ function generateOdds(len) { return arr.map(elem=>{ count+=2; return count; - }) + }); } @@ -146,7 +146,7 @@ function getUpperCaseStrings(arr) { function getStringsLength(arr) { return arr.map(elem =>{ return elem.length; - }); + }); } /** @@ -394,8 +394,8 @@ function getItemsSum(arr) { */ function getFalsyValuesCount(arr) { return arr.reduce((sum, cur)=>{ - if (!cur) return sum+=1 - else return sum + if (!cur) return sum+=1; + else return sum; },0); }; @@ -415,8 +415,8 @@ function getFalsyValuesCount(arr) { */ function findAllOccurences(arr, item) { return arr.reduce((sum, cur)=>{ - if (cur===item) return sum+=1 - else return sum + if (cur===item) return sum+=1; + else return sum; },0); }; @@ -514,7 +514,7 @@ function getIntervalArray(start, end) { let newArr=new Array(end - start +1).fill(undefined), count=start-1; return newArr.map(elem=>{ count++; - return count + return count; }); } @@ -534,7 +534,7 @@ function distinct(arr) { arr.reduce((sum, elem) => { if (!newArr.includes(elem)) { newArr.push(elem); - }},0) + }},0); return newArr; } From 38e87824b218594396eac379cd894053a1777fcf Mon Sep 17 00:00:00 2001 From: tapeghad Date: Sat, 11 Jul 2020 18:19:07 +0300 Subject: [PATCH 27/30] task_1_4 --- task/01-strings-tasks.js | 13 ++++--------- task/02-numbers-tasks.js | 5 ++--- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/task/01-strings-tasks.js b/task/01-strings-tasks.js index e31462797..07b1b685f 100644 --- a/task/01-strings-tasks.js +++ b/task/01-strings-tasks.js @@ -203,12 +203,9 @@ function extractEmails(str) { */ function getRectangleString(width, height) { let str="┌"+"─".repeat(width-2)+"┐"+'\n'; - let count_height=height-2; - while (count_height>0) - { + for (let count_height = height-2; count_height>0; count_height--){ str = str.concat("│"+" ".repeat(width-2))+"│\n"; - count_height--; - }; + } return str.concat("└"+"─".repeat(width-2)+"┘\n"); } @@ -229,9 +226,8 @@ function getRectangleString(width, height) { * */ function encodeToRot13(str) { - let arr=[], count=0; - while (count=78 & str.charCodeAt(count) <=90)| (str.charCodeAt(count) >=110 & str.charCodeAt(count)<=122)) { arr.push(str.charCodeAt(count)-13); @@ -241,7 +237,6 @@ function encodeToRot13(str) { arr.push(str.charCodeAt(count)+13); } else arr.push(str.charCodeAt(count)); - count++; }; return String.fromCharCode(...arr); } diff --git a/task/02-numbers-tasks.js b/task/02-numbers-tasks.js index 95bee57d7..4cc3f7f40 100644 --- a/task/02-numbers-tasks.js +++ b/task/02-numbers-tasks.js @@ -205,13 +205,12 @@ function roundToPowerOfTen(num, pow) { * 17 => true */ function isPrime(n) { - let check=true, count=2; - while (count <=n/2){ + let check=true; + for (let count=2; count<=n/2; count++){ if (n%count===0) {check = false; break; } - count++; } if (check===true) return true; else return false; From 047ddc86d251b7e9ffbdb14ab37e5fde42ef6fc7 Mon Sep 17 00:00:00 2001 From: tapeghad Date: Sat, 11 Jul 2020 18:45:12 +0300 Subject: [PATCH 28/30] task_1_4 --- task/01-strings-tasks.js | 1 - 1 file changed, 1 deletion(-) diff --git a/task/01-strings-tasks.js b/task/01-strings-tasks.js index 07b1b685f..e4de4f1d3 100644 --- a/task/01-strings-tasks.js +++ b/task/01-strings-tasks.js @@ -240,7 +240,6 @@ function encodeToRot13(str) { }; return String.fromCharCode(...arr); } - /** * Returns true if the value is string; otherwise false. * @param {string} value From a614d6a5925d19b2b5a583ef5a79a08557aa8364 Mon Sep 17 00:00:00 2001 From: tapeghad Date: Wed, 15 Jul 2020 21:23:13 +0300 Subject: [PATCH 29/30] task_1_4 --- task/03-date-tasks.js | 12 ++++++------ task/04-arrays-tasks.js | 36 ++++++++++++++++++------------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/task/03-date-tasks.js b/task/03-date-tasks.js index 9d38a83d6..d545644f1 100644 --- a/task/03-date-tasks.js +++ b/task/03-date-tasks.js @@ -79,10 +79,10 @@ function isLeapYear(date) { * Date(2000,1,1,10,0,0), Date(2000,1,1,15,20,10,453) => "05:20:10.453" */ function timeSpanToString(startDate, endDate) { - let time_period=endDate-startDate; - let milliseconds = parseInt((time_period % 1000)), seconds = Math.floor((time_period / 1000) % 60), - minutes = Math.floor((time_period / (1000 * 60)) % 60), - hours = Math.floor((time_period / (1000 * 60 * 60)) % 24); + let timePeriod=endDate-startDate; + let milliseconds = parseInt((timePeriod % 1000)), seconds = Math.floor((timePeriod / 1000) % 60), + minutes = Math.floor((timePeriod / (1000 * 60)) % 60), + hours = Math.floor((timePeriod / (1000 * 60 * 60)) % 24); hours = (startDate.getDay()!== endDate.getDay()) ? hours+24 : hours; hours = (hours < 10) ? "0" + hours : hours; minutes = (minutes < 10) ? "0" + minutes : minutes; @@ -107,8 +107,8 @@ function timeSpanToString(startDate, endDate) { * Date.UTC(2016,3,5,21, 0) => Math.PI/2 */ function angleBetweenClockHands(date) { - let angle_time=Math.abs((date.getHours()*60+date.getMinutes()+date.getTimezoneOffset()-date.getMinutes()*12)/2)%360; - return (angle_time<=180?angle_time:360-angle_time)/180*Math.PI; + let angleTime=Math.abs((date.getHours()*60+date.getMinutes()+date.getTimezoneOffset()-date.getMinutes()*12)/2)%360; + return (angleTime<=180?angleTime:360-angleTime)/180*Math.PI; } diff --git a/task/04-arrays-tasks.js b/task/04-arrays-tasks.js index 6d533a3bb..5ed89e035 100644 --- a/task/04-arrays-tasks.js +++ b/task/04-arrays-tasks.js @@ -293,8 +293,8 @@ function propagateItemsByPositionIndex(arr) { let newArr=[]; arr.map(elem=>{ let position=arr.indexOf(elem)+1; - let help_arr = new Array(position).fill(elem) - newArr = newArr.concat(help_arr) + let helpArr = new Array(position).fill(elem) + newArr = newArr.concat(helpArr) }); return newArr; } @@ -332,14 +332,14 @@ function get3TopItems(arr) { * [ 1, '2' ] => 1 */ function getPositivesCount(arr) { - let count_positive=0; + let countPositive=0; arr.map(elem=>{ if (typeof elem ==="number"){ - if (elem>0) count_positive++; + if (elem>0) countPositive++; } return elem; }) - return count_positive; + return countPositive; } /** @@ -356,9 +356,9 @@ function getPositivesCount(arr) { * [ 'one','one','one','zero' ] => [ 'zero','one','one','one' ] */ function sortDigitNamesByNumericOrder(arr) { - let Newarr=[{"name": "zero","number":0},{"name": "one","number":1}, {"name": "two","number":2}, {"name": "three","number":3}, {"name": "four","number":4}, {"name": "five","number":5}, {"name": "six","number":6}, {"name": "seven","number":7}, {"name": "eight","number":8}, {"name": "nine","number":9}]; + let newArr=[{"name": "zero","number":0},{"name": "one","number":1}, {"name": "two","number":2}, {"name": "three","number":3}, {"name": "four","number":4}, {"name": "five","number":5}, {"name": "six","number":6}, {"name": "seven","number":7}, {"name": "eight","number":8}, {"name": "nine","number":9}]; return arr.sort((a,b)=>{ - return (Newarr.find(x => x.name === a).number-Newarr.find(x => x.name === b).number); + return (newArr.find(x => x.name === a).number-newArr.find(x => x.name === b).number); }); } @@ -487,11 +487,11 @@ function sortCitiesArray(arr) { * [0,0,0,0,1]] */ function getIdentityMatrix(n) { - let matrix= new Array(n).fill(undefined), count_row=0; + let matrix= new Array(n).fill(undefined), countRow=0; matrix = matrix.map(elem=>{ let newArr=new Array(n).fill(0); - newArr[count_row]=1; - count_row++; + newArr[countRow]=1; + countRow++; return newArr; }); return matrix; @@ -637,19 +637,19 @@ function getElementByIndexes(arr, indexes) { * */ function swapHeadAndTail(arr) { - const arr_length = arr.length; - if (arr_length===1) return arr; - if (arr_length%2!=0){ + const arrLength = arr.length; + if (arrLength===1) return arr; + if (arrLength%2!=0){ let newArr=[]; - newArr=newArr.concat(arr.slice(Math.round(arr_length/2))); - newArr.push(arr[Math.floor(arr_length/2)]); - newArr=newArr.concat(arr.slice(0, Math.floor(arr_length/2))); + newArr=newArr.concat(arr.slice(Math.round(arrLength/2))); + newArr.push(arr[Math.floor(arrLength/2)]); + newArr=newArr.concat(arr.slice(0, Math.floor(arrLength/2))); return newArr; } else { let newArr=[]; - newArr= newArr.concat(arr.slice(arr_length/2)); - newArr=newArr.concat(arr.slice(0, arr_length/2)); + newArr= newArr.concat(arr.slice(arrLength/2)); + newArr=newArr.concat(arr.slice(0, arrLength/2)); return newArr; } } From 7bfdbdde1a0dc0e065f2a902d543f6dfc94dbec1 Mon Sep 17 00:00:00 2001 From: tapeghad Date: Fri, 24 Jul 2020 20:52:48 +0300 Subject: [PATCH 30/30] okay --- task/01-strings-tasks.js | 43 +++++++------ task/02-numbers-tasks.js | 32 +++++----- task/03-date-tasks.js | 15 +++-- task/04-arrays-tasks.js | 128 ++++++++++++++++++++------------------- 4 files changed, 112 insertions(+), 106 deletions(-) diff --git a/task/01-strings-tasks.js b/task/01-strings-tasks.js index e4de4f1d3..fea07bc03 100644 --- a/task/01-strings-tasks.js +++ b/task/01-strings-tasks.js @@ -22,7 +22,7 @@ * '', 'bb' => 'bb' */ function concatenateStrings(value1, value2) { - return value1+value2; + return value1 + value2; } @@ -69,8 +69,7 @@ function getStringFromTemplate(firstName, lastName) { * 'Hello, Chuck Norris!' => 'Chuck Norris' */ function extractNameFromTemplate(value) { - let name = value.slice(7); - return name.slice(0,-1); + return value.slice(7, -1); } @@ -100,7 +99,7 @@ function getFirstChar(value) { * '\tHello, World! ' => 'Hello, World!' */ function removeLeadingAndTrailingWhitespaces(value) { - return value.trim(/\t| |/g,''); + return value.trim( /\t| |/g,'' ); } /** @@ -146,7 +145,7 @@ function removeFirstOccurrences(str, value) { * '' => 'a' */ function unbracketTag(str) { - return str.replace(/<|>|/g,''); + return str.replace( /<|>|/g,'' ); } @@ -202,11 +201,12 @@ function extractEmails(str) { * */ function getRectangleString(width, height) { - let str="┌"+"─".repeat(width-2)+"┐"+'\n'; - for (let count_height = height-2; count_height>0; count_height--){ - str = str.concat("│"+" ".repeat(width-2))+"│\n"; + let str = "┌"+"─".repeat(width-2)+"┐"+'\n'; + + for (let countHeight = height - 2; countHeight > 0; countHeight -- ){ + str = str.concat("│" + " ".repeat(width-2)) + "│\n"; } - return str.concat("└"+"─".repeat(width-2)+"┘\n"); + return str.concat("└" + "─".repeat(width - 2)+"┘\n"); } @@ -226,19 +226,19 @@ function getRectangleString(width, height) { * */ function encodeToRot13(str) { - let arr=[]; - for (let count=0; count=78 & str.charCodeAt(count) <=90)| (str.charCodeAt(count) >=110 & str.charCodeAt(count)<=122)) + let finalArr = []; + + for (let count = 0; count < str.length; count ++ ){ + if ((str.charCodeAt(count) >= 78 & str.charCodeAt(count) <= 90) | (str.charCodeAt(count) >= 110 & str.charCodeAt(count) <= 122)) { - arr.push(str.charCodeAt(count)-13); - } - else if ((str.charCodeAt(count) >=65 & str.charCodeAt(count) <=77)| (str.charCodeAt(count) >=97 & str.charCodeAt(count)<=109)) + finalArr.push(str.charCodeAt(count) - 13); + } else + if ((str.charCodeAt(count) >= 65 & str.charCodeAt(count) <= 77)| (str.charCodeAt(count) >= 97 & str.charCodeAt(count) <= 109)) { - arr.push(str.charCodeAt(count)+13); - } - else arr.push(str.charCodeAt(count)); + finalArr.push(str.charCodeAt(count) + 13); + } else finalArr.push(str.charCodeAt(count)); }; - return String.fromCharCode(...arr); + return String.fromCharCode(...finalArr); } /** * Returns true if the value is string; otherwise false. @@ -254,8 +254,7 @@ function encodeToRot13(str) { * isString(new String('test')) => true */ function isString(value) { - if (typeof value ==="string" || value instanceof String) return true; - else return false; + return (typeof value ==="string" || value instanceof String); } @@ -284,7 +283,7 @@ function isString(value) { * 'K♠' => 51 */ function getCardId(value) { - let arr=['A♣','2♣','3♣','4♣','5♣','6♣','7♣','8♣','9♣','10♣','J♣','Q♣','K♣', + let arr = ['A♣','2♣','3♣','4♣','5♣','6♣','7♣','8♣','9♣','10♣','J♣','Q♣','K♣', 'A♦','2♦','3♦','4♦','5♦','6♦','7♦','8♦','9♦','10♦','J♦','Q♦','K♦', 'A♥','2♥','3♥','4♥','5♥','6♥','7♥','8♥','9♥','10♥','J♥','Q♥','K♥', 'A♠','2♠','3♠','4♠','5♠','6♠','7♠','8♠','9♠','10♠','J♠','Q♠','K♠']; diff --git a/task/02-numbers-tasks.js b/task/02-numbers-tasks.js index 4cc3f7f40..3002586f9 100644 --- a/task/02-numbers-tasks.js +++ b/task/02-numbers-tasks.js @@ -24,7 +24,7 @@ * 5, 5 => 25 */ function getRectangleArea(width, height) { - return width*height; + return width * height; } @@ -40,7 +40,7 @@ function getRectangleArea(width, height) { * 0 => 0 */ function getCicleCircumference(radius) { - return 2*radius*Math.PI; + return 2 * radius * Math.PI; } /** @@ -56,7 +56,7 @@ function getCicleCircumference(radius) { * -3, 3 => 0 */ function getAverage(value1, value2) { - return value1/2 +value2/2; + return value1 / 2 + value2 / 2; } /** @@ -75,7 +75,7 @@ function getAverage(value1, value2) { * (-5,0) (10,-10) => 18.027756377319946 */ function getDistanceBetweenPoints(x1, y1, x2, y2) { - return Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2)); + return Math.hypot(x1 - x2, y1 - y2); } /** @@ -91,7 +91,7 @@ function getDistanceBetweenPoints(x1, y1, x2, y2) { * 5*x = 0 => 0 */ function getLinearEquationRoot(a, b) { - return -b/a; + return -b / a; } @@ -113,7 +113,8 @@ function getLinearEquationRoot(a, b) { * (0,1) (1,2) => 0 */ function getAngleBetweenVectors(x1, y1, x2, y2) { - return Math.acos((x1*x2+y1*y2)/(Math.sqrt(Math.pow(x1,2)+Math.pow(y1,2)) * Math.sqrt(Math.sqrt(Math.pow(x2,2)+Math.pow(y2,2))))); + return Math.acos((x1 * x2 + y1 * y2) / Math.hypot(x1, y1) * + Math.sqrt( Math.hypot(x2, y2))); } /** @@ -129,7 +130,7 @@ function getAngleBetweenVectors(x1, y1, x2, y2) { * 0 => 0 */ function getLastDigit(value) { - return value%10; + return value % 10; } @@ -162,7 +163,7 @@ function parseNumberFromString(value) { * 1,2,3 => 3.741657386773941 */ function getParallelipidedDiagonal(a,b,c) { - return Math.sqrt(Math.pow(a,2) +Math.pow(b,2)+Math.pow(c,2)); + return Math.hypot(a, b, c); } /** @@ -183,7 +184,7 @@ function getParallelipidedDiagonal(a,b,c) { * 1678, 3 => 2000 */ function roundToPowerOfTen(num, pow) { - return Math.round(num/10**pow)*10**pow; + return Math.round( num / 10 ** pow) * 10 ** pow; } @@ -205,15 +206,12 @@ function roundToPowerOfTen(num, pow) { * 17 => true */ function isPrime(n) { - let check=true; - for (let count=2; count<=n/2; count++){ - if (n%count===0) - {check = false; - break; + for ( let count = 2; count <= n / 2; count ++ ){ + if (n % count === 0) { + return false; } } - if (check===true) return true; - else return false; + return true; } /** @@ -232,7 +230,7 @@ function isPrime(n) { * toNumber(new Number(42), 0) => 42 */ function toNumber(value, def) { - if (parseInt(value)) return parseInt(value); + if ( parseInt(value) ) return parseInt(value); return def; } diff --git a/task/03-date-tasks.js b/task/03-date-tasks.js index d545644f1..db8612bd2 100644 --- a/task/03-date-tasks.js +++ b/task/03-date-tasks.js @@ -56,9 +56,9 @@ function parseDataFromIso8601(value) { * Date(2015,1,1) => false */ function isLeapYear(date) { - if (date.getFullYear()%4!=0) return false; - else if (date.getFullYear()%100!=0) return true; - else if (date.getFullYear()%400!=0) return false; + if (date.getFullYear() % 4 != 0) return false; + else if (date.getFullYear() % 100 != 0) return true; + else if (date.getFullYear() % 400 != 0) return false; else return true; } @@ -79,7 +79,7 @@ function isLeapYear(date) { * Date(2000,1,1,10,0,0), Date(2000,1,1,15,20,10,453) => "05:20:10.453" */ function timeSpanToString(startDate, endDate) { - let timePeriod=endDate-startDate; + let timePeriod = endDate-startDate; let milliseconds = parseInt((timePeriod % 1000)), seconds = Math.floor((timePeriod / 1000) % 60), minutes = Math.floor((timePeriod / (1000 * 60)) % 60), hours = Math.floor((timePeriod / (1000 * 60 * 60)) % 24); @@ -107,8 +107,11 @@ function timeSpanToString(startDate, endDate) { * Date.UTC(2016,3,5,21, 0) => Math.PI/2 */ function angleBetweenClockHands(date) { - let angleTime=Math.abs((date.getHours()*60+date.getMinutes()+date.getTimezoneOffset()-date.getMinutes()*12)/2)%360; - return (angleTime<=180?angleTime:360-angleTime)/180*Math.PI; + let hours = date.getHours(); + let minutes = date.getMinutes(); + let timeZone = date.getTimezoneOffset(); + let angleTime = Math.abs((hours * 60 + minutes + timeZone - minutes * 12) / 2) % 360; + return (angleTime <= 180 ? angleTime : 360 - angleTime ) / 180 * Math.PI; } diff --git a/task/04-arrays-tasks.js b/task/04-arrays-tasks.js index 5ed89e035..cf745812f 100644 --- a/task/04-arrays-tasks.js +++ b/task/04-arrays-tasks.js @@ -23,8 +23,7 @@ * [0, 1, 2, 3, 4, 5], 5 => 5 */ function findElement(arr, value) { - if (arr.includes(value)) return arr.indexOf(value); - else return -1; + return arr.indexOf(value); } /** @@ -39,9 +38,10 @@ function findElement(arr, value) { * 5 => [ 1, 3, 5, 7, 9 ] */ function generateOdds(len) { - let arr=new Array(len).fill(undefined), count = -1; - return arr.map(elem=>{ - count+=2; + let arr = new Array(len).fill(undefined); + let count = -1; + return arr.map( elem=> { + count += 2; return count; }); } @@ -75,8 +75,8 @@ function doubleArray(arr) { * [] => [] */ function getArrayOfPositives(arr) { - return arr.filter(elem =>{ - return elem>0; + return arr.filter( elem => { + return elem > 0; }); } @@ -92,8 +92,8 @@ function getArrayOfPositives(arr) { * [ 'cat, 'dog', 'raccon' ] => [ 'cat', 'dog', 'racoon' ] */ function getArrayOfStrings(arr) { - return arr.filter(elem =>{ - return typeof elem ==="string"; + return arr.filter( elem =>{ + return typeof elem === "string"; }); } @@ -111,7 +111,7 @@ function getArrayOfStrings(arr) { * [ false, 0, NaN, '', undefined ] => [ ] */ function removeFalsyValues(arr) { - return arr.filter(elem =>{ + return arr.filter( elem => { if (elem) return elem; }); } @@ -127,9 +127,7 @@ function removeFalsyValues(arr) { * [ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ] => [ 'A', 'B', 'C', 'D', 'E', 'F', 'G' ] */ function getUpperCaseStrings(arr) { - return arr.map(elem =>{ - return elem.toUpperCase(); - }); + return arr.map( elem => elem.toUpperCase()); } @@ -144,7 +142,7 @@ function getUpperCaseStrings(arr) { * [ 'angular', 'react', 'ember' ] => [ 7, 5, 5 ] */ function getStringsLength(arr) { - return arr.map(elem =>{ + return arr.map( elem => { return elem.length; }); } @@ -215,7 +213,7 @@ function getTail(arr, n) { * +'30,31,32,33,34' */ function toCsvText(arr) { - return arr.join("\n"); + return arr.join( "\n" ); } /** @@ -230,7 +228,9 @@ function toCsvText(arr) { * [ 10, 100, -1 ] => [ 100, 10000, 1 ] */ function toArrayOfSquares(arr) { - return arr.map(elem=>{return Math.pow(elem, 2)}); + return arr.map( elem => { + return Math.pow(elem, 2); + }); } @@ -249,11 +249,11 @@ function toArrayOfSquares(arr) { * [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] => [ 1, 3, 6, 10, 15, 21, 28, 36, 45, 55 ] */ function getMovingSum(arr) { - let newArr=[]; + let newArr = []; arr.reduce(function(sum, current) { - newArr.push(sum+current); + newArr.push(sum + current); return sum + current; - },0); + }, 0); return newArr; } @@ -269,8 +269,8 @@ function getMovingSum(arr) { * [ "a" ] => [] */ function getSecondItems(arr) { - return arr.filter(elem=>{ - return arr.indexOf(elem)%2===1; + return arr.filter( elem => { + return arr.indexOf(elem) % 2 === 1; }); } @@ -290,11 +290,11 @@ function getSecondItems(arr) { * [ 1,2,3,4,5 ] => [ 1, 2,2, 3,3,3, 4,4,4,4, 5,5,5,5,5 ] */ function propagateItemsByPositionIndex(arr) { - let newArr=[]; - arr.map(elem=>{ - let position=arr.indexOf(elem)+1; - let helpArr = new Array(position).fill(elem) - newArr = newArr.concat(helpArr) + let newArr = []; + arr.map(elem => { + let position = arr.indexOf(elem) + 1; + let helpArr = new Array(position).fill(elem); + newArr = newArr.concat(helpArr); }); return newArr; } @@ -314,7 +314,9 @@ function propagateItemsByPositionIndex(arr) { * [ 10, 10, 10, 10 ] => [ 10, 10, 10 ] */ function get3TopItems(arr) { - return arr.sort((a, b)=>{return b-a}).slice(0,3); + return arr.sort( (a, b) => { + return b-a; + }).slice(0,3); } @@ -332,10 +334,10 @@ function get3TopItems(arr) { * [ 1, '2' ] => 1 */ function getPositivesCount(arr) { - let countPositive=0; - arr.map(elem=>{ - if (typeof elem ==="number"){ - if (elem>0) countPositive++; + let countPositive = 0; + arr.map( elem => { + if (typeof elem === "number"){ + if (elem > 0) countPositive ++; } return elem; }) @@ -356,9 +358,9 @@ function getPositivesCount(arr) { * [ 'one','one','one','zero' ] => [ 'zero','one','one','one' ] */ function sortDigitNamesByNumericOrder(arr) { - let newArr=[{"name": "zero","number":0},{"name": "one","number":1}, {"name": "two","number":2}, {"name": "three","number":3}, {"name": "four","number":4}, {"name": "five","number":5}, {"name": "six","number":6}, {"name": "seven","number":7}, {"name": "eight","number":8}, {"name": "nine","number":9}]; - return arr.sort((a,b)=>{ - return (newArr.find(x => x.name === a).number-newArr.find(x => x.name === b).number); + let numbersArr = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; + return arr.sort((a, b)=>{ + return numbersArr.indexOf(a) - numbersArr.indexOf(b); }); } @@ -376,8 +378,8 @@ function sortDigitNamesByNumericOrder(arr) { */ function getItemsSum(arr) { return arr.reduce((sum, cur)=>{ - return sum+cur - },0); + return sum+cur; + }, 0); }; /** @@ -394,9 +396,9 @@ function getItemsSum(arr) { */ function getFalsyValuesCount(arr) { return arr.reduce((sum, cur)=>{ - if (!cur) return sum+=1; + if (!cur) return sum += 1; else return sum; - },0); + }, 0); }; /** @@ -415,9 +417,9 @@ function getFalsyValuesCount(arr) { */ function findAllOccurences(arr, item) { return arr.reduce((sum, cur)=>{ - if (cur===item) return sum+=1; + if (cur === item) return sum += 1; else return sum; - },0); + }, 0); }; /** @@ -461,7 +463,7 @@ function toStringList(arr) { * { country: 'Russia', city: 'Saint Petersburg' } */ function sortCitiesArray(arr) { - return arr.sort((a,b)=>{ + return arr.sort((a, b)=>{ if(a.country < b.country) { return -1; } if(a.country > b.country) { return 1; } if(a.city < b.city) { return -1; } @@ -487,14 +489,15 @@ function sortCitiesArray(arr) { * [0,0,0,0,1]] */ function getIdentityMatrix(n) { - let matrix= new Array(n).fill(undefined), countRow=0; - matrix = matrix.map(elem=>{ - let newArr=new Array(n).fill(0); - newArr[countRow]=1; + let finalMatrix = new Array(n).fill(undefined); + let countRow = 0; + finalMatrix = finalMatrix.map( elem => { + let newArr = new Array(n).fill(0); + newArr[countRow] = 1; countRow++; return newArr; }); - return matrix; + return finalMatrix; } /** @@ -511,8 +514,9 @@ function getIdentityMatrix(n) { * 3, 3 => [ 3 ] */ function getIntervalArray(start, end) { - let newArr=new Array(end - start +1).fill(undefined), count=start-1; - return newArr.map(elem=>{ + let newArr = new Array(end - start + 1).fill(undefined); + let count = start - 1; + return newArr.map( elem => { count++; return count; }); @@ -534,7 +538,7 @@ function distinct(arr) { arr.reduce((sum, elem) => { if (!newArr.includes(elem)) { newArr.push(elem); - }},0); + }}, 0); return newArr; } @@ -571,7 +575,7 @@ function distinct(arr) { */ function group(array, keySelector, valueSelector) { const map = new Map(); - array.map((item) => { + array.map( (item) => { const key = keySelector(item); const value = valueSelector(item); const collection = map.get(key); @@ -613,8 +617,10 @@ function selectMany(arr, childrenSelector) { * [[[ 1, 2, 3]]], [ 0, 0, 1 ] => 2 (arr[0][0][1]) */ function getElementByIndexes(arr, indexes) { - let place=indexes.map(elem=>{return `[${elem}]`}).join(""); - return eval(`arr${place}`); + indexes.map(index => { + arr = arr[index]; + }); + return arr; } @@ -638,18 +644,18 @@ function getElementByIndexes(arr, indexes) { */ function swapHeadAndTail(arr) { const arrLength = arr.length; - if (arrLength===1) return arr; - if (arrLength%2!=0){ - let newArr=[]; - newArr=newArr.concat(arr.slice(Math.round(arrLength/2))); - newArr.push(arr[Math.floor(arrLength/2)]); - newArr=newArr.concat(arr.slice(0, Math.floor(arrLength/2))); + if (arrLength === 1) return arr; + if (arrLength % 2 != 0){ + let newArr = []; + newArr=newArr.concat(arr.slice(Math.round(arrLength / 2))); + newArr.push(arr[Math.floor(arrLength / 2)]); + newArr=newArr.concat(arr.slice(0, Math.floor(arrLength / 2))); return newArr; } else { - let newArr=[]; - newArr= newArr.concat(arr.slice(arrLength/2)); - newArr=newArr.concat(arr.slice(0, arrLength/2)); + let newArr = []; + newArr= newArr.concat(arr.slice(arrLength / 2)); + newArr=newArr.concat(arr.slice(0, arrLength / 2)); return newArr; } }