diff --git a/.travis.yml b/.travis.yml index 008cbc167..b0e500072 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ language: node_js node_js: - - "5.10.0" + - "12.18.2" diff --git a/README.md b/README.md index 79f9dff75..e629099ac 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ To start javascript assignments please follow the next steps: 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/AlekseyPrusevich/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 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..1f387c39b 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,8 @@ "devDependencies": { "mocha": "^2.3.4" }, - "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..980482853 100644 --- a/task/01-strings-tasks.js +++ b/task/01-strings-tasks.js @@ -12,7 +12,7 @@ /** * Returns the result of concatenation of two strings. * - * @param {string} value1 + * @param {string} value1 * @param {string} value2 * @return {string} * @@ -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,7 @@ function getStringFromTemplate(firstName, lastName) { * 'Hello, Chuck Norris!' => 'Chuck Norris' */ function extractNameFromTemplate(value) { - throw new Error('Not implemented'); + return value.replace('Hello, ', '').replace('!', ''); } @@ -84,7 +84,7 @@ function extractNameFromTemplate(value) { * 'cat' => 'c' */ function getFirstChar(value) { - throw new Error('Not implemented'); + return value.charAt(0); } /** @@ -99,7 +99,7 @@ function getFirstChar(value) { * '\tHello, World! ' => 'Hello, World!' */ function removeLeadingAndTrailingWhitespaces(value) { - throw new Error('Not implemented'); + return value.trim(); } /** @@ -114,7 +114,7 @@ function removeLeadingAndTrailingWhitespaces(value) { * 'cat', 3 => 'catcatcat' */ function repeatString(value, count) { - throw new Error('Not implemented'); + return value.repeat(count); } /** @@ -130,7 +130,7 @@ function repeatString(value, count) { * 'ABABAB','BA' => 'ABAB' */ function removeFirstOccurrences(str, value) { - throw new Error('Not implemented'); + return str.replace(value, ''); } /** @@ -145,7 +145,8 @@ function removeFirstOccurrences(str, value) { * '' => 'a' */ function unbracketTag(str) { - throw new Error('Not implemented'); + return str.replace(/[<>]/g, ''); + // return str.replace(str[0], '').replace(str[str.length-1], ''); } @@ -160,7 +161,7 @@ function unbracketTag(str) { * 'abcdefghijklmnopqrstuvwxyz' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' */ function convertToUpperCase(str) { - throw new Error('Not implemented'); + return str.toUpperCase(str); } /** @@ -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,9 @@ function extractEmails(str) { * */ function getRectangleString(width, height) { - throw new Error('Not implemented'); + return '┌' + '─'.repeat(width-2) + '┐' + '\n' + + ('│' + ' '.repeat(width-2) + '│\n').repeat(height-2) + + '└' + '─'.repeat(width-2) + '┘' + '\n'; } @@ -221,7 +224,9 @@ function getRectangleString(width, height) { * */ function encodeToRot13(str) { - throw new Error('Not implemented'); + return str.replace(/[a-zA-Z]/g, function (char) { + return String.fromCharCode((char <= "Z" ? 90 : 122) >= (char = char.charCodeAt(0) + 13) ? char : char - 26); + }); } /** @@ -238,7 +243,7 @@ function encodeToRot13(str) { * isString(new String('test')) => true */ function isString(value) { - throw new Error('Not implemented'); + return value ? typeof value.valueOf() == 'string' : false; } @@ -267,7 +272,9 @@ function isString(value) { * 'K♠' => 51 */ function getCardId(value) { - throw new Error('Not implemented'); + let cardsArr = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']; + let suitsArr = ['♣', '♦', '♥', '♠']; + return cardsArr.indexOf(value.slice(0, -1)) + suitsArr.indexOf(value.slice(-1)) * cardsArr.length; } diff --git a/task/02-numbers-tasks.js b/task/02-numbers-tasks.js index c9ed20208..eb79c7b7a 100644 --- a/task/02-numbers-tasks.js +++ b/task/02-numbers-tasks.js @@ -22,7 +22,7 @@ * 5, 5 => 25 */ function getRectangleArea(width, height) { - throw new Error('Not implemented'); + return width*height; } @@ -38,7 +38,7 @@ function getRectangleArea(width, height) { * 0 => 0 */ function getCicleCircumference(radius) { - throw new Error('Not implemented'); + return 2 * Math.PI * radius; } /** @@ -54,7 +54,7 @@ function getCicleCircumference(radius) { * -3, 3 => 0 */ function getAverage(value1, value2) { - throw new Error('Not implemented'); + return value1 / 2 + value2 / 2; } /** @@ -73,7 +73,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 +89,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 +111,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.pow(x2, 2) + Math.pow(y2, 2)))); } /** @@ -127,7 +127,7 @@ function getAngleBetweenVectors(x1, y1, x2, y2) { * 0 => 0 */ function getLastDigit(value) { - throw new Error('Not implemented'); + return value % 10; } @@ -143,7 +143,7 @@ function getLastDigit(value) { * '-525.5' => -525.5 */ function parseNumberFromString(value) { - throw new Error('Not implemented'); + return +value; } /** @@ -160,7 +160,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 +181,7 @@ 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 +202,12 @@ function roundToPowerOfTen(num, pow) { * 17 => true */ function isPrime(n) { - throw new Error('Not implemented'); + for (let i = 2; i < n; i++) { + if (n % i === 0) { + return false; + } + } + return true; } /** @@ -221,7 +226,7 @@ function isPrime(n) { * toNumber(new Number(42), 0) => 42 */ function toNumber(value, def) { - throw new Error('Not implemented'); + return +value ? value : def; } module.exports = { diff --git a/task/03-date-tasks.js b/task/03-date-tasks.js index 83c6266bc..5b902a153 100644 --- a/task/03-date-tasks.js +++ b/task/03-date-tasks.js @@ -22,7 +22,9 @@ * 'Sun, 17 May 1998 03:00:00 GMT+01' => Date() */ function parseDataFromRfc2822(value) { - throw new Error('Not implemented'); + return Date.parse(value); + + //throw new Error('Not implemented'); } /** @@ -37,7 +39,9 @@ function parseDataFromRfc2822(value) { * '2016-01-19T08:07:37Z' => Date() */ function parseDataFromIso8601(value) { - throw new Error('Not implemented'); + return Date.parse(value); + + //throw new Error('Not implemented'); } @@ -56,7 +60,13 @@ function parseDataFromIso8601(value) { * Date(2015,1,1) => false */ function isLeapYear(date) { - throw new Error('Not implemented'); + let year = date.getFullYear(); + + return (+year % 4 != 0) ? false : + (+year % 100 != 0) ? true : + (+year % 400 != 0) ? false : true; + + //throw new Error('Not implemented'); } @@ -76,7 +86,21 @@ 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 dateDif = endDate - startDate; + let date = new Date(); + + let hour = Math.floor(dateDif / 3.6e6); + let min = Math.floor((dateDif - hour * 3.6e6) / 6e4); + let sec = Math.floor((dateDif - hour * 3.6e6 - min * 6e4) / 1e3); + let ms = dateDif - hour * 3.6e6 - min * 6e4 - sec * 1e3; +/* + date.setHours(Math.floor(dateDif / 3.6e6)); + date.setMinutes(Math.floor((dateDif % 3.6e6) / 6e4)); + date.setSegeconds(Math.floor((dateDif % 3.6e6 % 6e4) / 1e3)); + date.setMilliseconds(dateDif % 3.6e6 % 6e4 % 1e3); +*/ + return ('0' + hour).slice(-2) + ':' + ('0' + min).slice(-2) + ':' + ('0' + sec).slice(-2) + '.' + (ms + '00').slice(0, 3); } @@ -88,16 +112,72 @@ function timeSpanToString(startDate, endDate) { * @return {number} * * @example: + * * Date.UTC(2016,2,5, 0, 0) => 0 * Date.UTC(2016,3,5, 3, 0) => Math.PI/2 * Date.UTC(2016,3,5,18, 0) => Math.PI * Date.UTC(2016,3,5,21, 0) => Math.PI/2 */ function angleBetweenClockHands(date) { - throw new Error('Not implemented'); +/* + + let minuteDegree = 0; + let hourDegree = 0; + + let newDate = new Date(date); + + minuteDegree = newDate.getMinutes() * (360 / 60); + minuteDegree != 0 ? hourDegree = newDate.getHours() * (360 / 12) + ((360 / 12) / (60 / minuteDegree)) : hourDegree = newDate.getHours() * (360 / 12); + + console.log("Время - " + date); + console.log("Минутная - " + minuteDegree); + console.log("Часовая - " + hourDegree); + + console.log("Градусы - " + Math.sin(Math.abs(minuteDegree - hourDegree))); + console.log(" "); + + return Math.sin(Math.abs(minuteDegree - hourDegree)); + + let degree = 0.5 * (60 * date.getUTCHours() - 11 * date.getUTCHours()); + let totalDegree = degree > 180 ? 360 - degree : degree; + return Math.PI/180 * totalDegree; + */ + + let map = new Map(); + let newDate = new Date(date); + let hour = newDate.getUTCHours(); + let min = newDate.getUTCMinutes(); + let hour2; + let mih; + let ang; + let result; + + if (hour >= 12) hour -= 12; + + map.set(0, 0) + .set(30, Math.PI / 6) + .set(45, Math.PI / 4) + .set(60, Math.PI / 3) + .set(90, Math.PI / 2) + .set(180, Math.PI); + + hour2 = 0.5 * (60 * hour + min); + mih = 6 * min; + ang = Math.abs(hour2 - mih); + + if (ang > 180) ang -= 180; + + result = map.get(Math.abs(ang)); + + if (result == null && result == undefined) + return ang * Math.PI / 180 + else + return result; + throw new Error('Not implemented'); } + module.exports = { parseDataFromRfc2822: parseDataFromRfc2822, parseDataFromIso8601: parseDataFromIso8601, diff --git a/task/04-arrays-tasks.js b/task/04-arrays-tasks.js index ff3a4c019..784ba2233 100644 --- a/task/04-arrays-tasks.js +++ b/task/04-arrays-tasks.js @@ -1,5 +1,7 @@ 'use strict'; +const { parseDataFromRfc2822 } = require("./03-date-tasks"); + /********************************************************************************************* * * * Plese read the following tutorial before implementing tasks: * @@ -23,7 +25,7 @@ * [0, 1, 2, 3, 4, 5], 5 => 5 */ function findElement(arr, value) { - throw new Error('Not implemented'); + return arr.indexOf(value); } /** @@ -38,7 +40,9 @@ function findElement(arr, value) { * 5 => [ 1, 3, 5, 7, 9 ] */ function generateOdds(len) { - throw new Error('Not implemented'); + return new Array(len).fill(0).map(function(_, i) { + return 2 * i + 1; + }); } @@ -54,7 +58,7 @@ function generateOdds(len) { * [] => [] */ function doubleArray(arr) { - throw new Error('Not implemented'); + return [...arr, ...arr]; } @@ -70,7 +74,7 @@ function doubleArray(arr) { * [] => [] */ function getArrayOfPositives(arr) { - throw new Error('Not implemented'); + return arr.filter(arr => arr > 0); } /** @@ -85,7 +89,7 @@ function getArrayOfPositives(arr) { * [ 'cat, 'dog', 'raccon' ] => [ 'cat', 'dog', 'racoon' ] */ function getArrayOfStrings(arr) { - throw new Error('Not implemented'); + return arr.filter(arr => typeof arr == "string"); } /** @@ -102,7 +106,7 @@ function getArrayOfStrings(arr) { * [ false, 0, NaN, '', undefined ] => [ ] */ function removeFalsyValues(arr) { - throw new Error('Not implemented'); + return arr.filter(arr => Boolean(arr) == true ); } /** @@ -116,7 +120,7 @@ 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((element, _) => element.toUpperCase()); } @@ -131,7 +135,7 @@ function getUpperCaseStrings(arr) { * [ 'angular', 'react', 'ember' ] => [ 7, 5, 5 ] */ function getStringsLength(arr) { - throw new Error('Not implemented'); + return arr.map((element) => element.length); } /** @@ -146,7 +150,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 +164,9 @@ function insertItem(arr, item, index) { * [ 'a', 'b', 'c', 'd'], 3 => [ 'a', 'b', 'c' ] */ function getHead(arr, n) { - throw new Error('Not implemented'); + let delered = arr.splice(n); + + return arr; } @@ -175,7 +181,7 @@ function getHead(arr, n) { * [ 'a', 'b', 'c', 'd'], 3 => [ 'b', 'c', 'd' ] */ function getTail(arr, n) { - throw new Error('Not implemented'); + return arr.splice(-n); } @@ -200,7 +206,9 @@ function getTail(arr, n) { * +'30,31,32,33,34' */ function toCsvText(arr) { - throw new Error('Not implemented'); + let newArr = arr.map(element => element.join(',') + '\n').join(''); + + return newArr.slice(0, newArr.length - 1); } /** @@ -215,7 +223,7 @@ function toCsvText(arr) { * [ 10, 100, -1 ] => [ 100, 10000, 1 ] */ function toArrayOfSquares(arr) { - throw new Error('Not implemented'); + return arr.map((element) => element ** 2); } @@ -234,7 +242,21 @@ 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'); + //return arr.map((element, index) => arr[index] == arr[0] ? arr[index] = arr[index] : arr[index] = arr[index] + arr[index-1]); + let pastElem; + return arr.map(function(elem, index) { + if (index == 0) + { + pastElem = elem; + return elem; + } + else + { + elem = elem + pastElem; + pastElem = elem; + return elem; + } + }); } /** @@ -249,7 +271,7 @@ function getMovingSum(arr) { * [ "a" ] => [] */ function getSecondItems(arr) { - throw new Error('Not implemented'); + return arr.filter((_, index) => index % 2 != 0); } @@ -268,7 +290,14 @@ 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.filter((element, index) => { + let insArr = new Array(index + 1); + insArr.fill(element); + newArr.push(...insArr); + }); + + return newArr; } @@ -286,7 +315,7 @@ function propagateItemsByPositionIndex(arr) { * [ 10, 10, 10, 10 ] => [ 10, 10, 10 ] */ function get3TopItems(arr) { - throw new Error('Not implemented'); + return arr.splice(-3).reverse(); } @@ -304,7 +333,16 @@ function get3TopItems(arr) { * [ 1, '2' ] => 1 */ function getPositivesCount(arr) { - throw new Error('Not implemented'); + let count = 0; + arr.map(function(element) { + if (typeof element == "number" && element > 0) + { + count++; + return element; + } + }); + + return count; } /** @@ -321,7 +359,9 @@ function getPositivesCount(arr) { * [ 'one','one','one','zero' ] => [ 'zero','one','one','one' ] */ function sortDigitNamesByNumericOrder(arr) { - throw new Error('Not implemented'); + const numbers = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']; + + return arr.sort((firstElement, secondElement) => { return numbers.indexOf(firstElement) - numbers.indexOf(secondElement) }); } /** @@ -337,7 +377,13 @@ function sortDigitNamesByNumericOrder(arr) { * [ 1, 10, 100, 1000 ] => 1111 */ function getItemsSum(arr) { - throw new Error('Not implemented'); + let sum = 0; + arr.map((element) => { + sum = element + sum; + return element; + }); + + return sum; } /** @@ -353,7 +399,16 @@ function getItemsSum(arr) { * [ null, undefined, NaN, false, 0, '' ] => 6 */ function getFalsyValuesCount(arr) { - throw new Error('Not implemented'); + let count = 0; + arr.map((element) => { + if (Boolean(element) == false) + { + count++; + return element; + } + }); + + return count; } /** @@ -371,7 +426,16 @@ function getFalsyValuesCount(arr) { * [ true, 0, 1, 'true' ], true => 1 */ function findAllOccurences(arr, item) { - throw new Error('Not implemented'); + let count = 0; + arr.map((element) => { + if (element === item) + { + count++; + return element; + } + }); + + return count; } /** @@ -386,7 +450,20 @@ function findAllOccurences(arr, item) { * ['rock', 'paper', 'scissors'] => 'rock,paper,scissors' */ function toStringList(arr) { - throw new Error('Not implemented'); + let str = ''; + arr.map((element, index) => { + if (index + 1 != arr.length) + { + str = str + element + ','; + } + else + { + str = str + element; + } + return element; + }); + + return str; } @@ -405,7 +482,7 @@ function toStringList(arr) { * { country: 'Poland', city: 'Krakow' }, * { country: 'Belarus', city: 'Brest' } * ] - * => + * => prev.country > next.country || prev.city > next.city * [ * { country: 'Belarus', city: 'Brest' }, * { country: 'Belarus', city: 'Minsk' }, @@ -415,7 +492,7 @@ function toStringList(arr) { * { country: 'Russia', city: 'Saint Petersburg' } */ function sortCitiesArray(arr) { - throw new Error('Not implemented'); + return arr.sort((prev, next) => ((prev.country > next.country) - (next.country > prev.country) || (prev.city > next.city) - (next.city > prev.city))); } /** @@ -437,7 +514,12 @@ function sortCitiesArray(arr) { * [0,0,0,0,1]] */ function getIdentityMatrix(n) { - throw new Error('Not implemented'); + let newArr = new Array(n).fill(0); + return newArr.map((element, index) => { + let rowArr = new Array(n).fill(0); + rowArr[index] = 1; + return element = rowArr; + }); } /** @@ -454,7 +536,15 @@ function getIdentityMatrix(n) { * 3, 3 => [ 3 ] */ function getIntervalArray(start, end) { - throw new Error('Not implemented'); + let lengthArr = end - start + 1; + let arr = []; + arr.length = lengthArr; + arr.fill(start); + return arr.map(element => { + element = start; + start = start + 1; + return element; + }); } /** @@ -469,7 +559,7 @@ function getIntervalArray(start, end) { * [ 1, 1, 2, 2, 3, 3, 4, 4] => [ 1, 2, 3, 4] */ function distinct(arr) { - throw new Error('Not implemented'); + return arr.filter((element, index) => arr.indexOf(element) === index); } /** @@ -503,7 +593,24 @@ function distinct(arr) { * } */ function group(array, keySelector, valueSelector) { - throw new Error('Not implemented'); + let key = array.map(keySelector); + let value = array.map(valueSelector); + let map = new Map(); + + key.filter((element, index) => { + if (!map.has(element)) + { + map.set(element, [value[index]]); + } + else + { + let value2 = map.get(element); + value2.push(value[index]); + map.set(element, value2); + } + }); + + return map; } @@ -519,7 +626,12 @@ 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'); + let newArr = []; + arr.map(element => { + newArr.push(...childrenSelector(element)); + }); + + return newArr; } @@ -536,7 +648,7 @@ function selectMany(arr, childrenSelector) { * [[[ 1, 2, 3]]], [ 0, 0, 1 ] => 2 (arr[0][0][1]) */ function getElementByIndexes(arr, indexes) { - throw new Error('Not implemented'); + return indexes.reduce((newArr, index) => newArr[index], arr); } @@ -559,7 +671,14 @@ function getElementByIndexes(arr, indexes) { * */ function swapHeadAndTail(arr) { - throw new Error('Not implemented'); + let arr2; + let lenghtArr = arr.length; + let head = arr.slice(0, (lenghtArr / 2)); + let tail = arr.slice( -(lenghtArr / 2)); + + if (lenghtArr % 2 != 0 && lenghtArr > 1) return arr2 = [...tail, arr[Math.floor(lenghtArr / 2)], ...head]; + + return arr2 = [...tail, ...head];; }