From 693900ef79e4b71558f93388afe531a87656e07d Mon Sep 17 00:00:00 2001 From: VItaliy Avseikov <78975759+qusewen@users.noreply.github.com> Date: Fri, 25 Nov 2022 21:39:18 +0300 Subject: [PATCH] update solution --- src/01-strings-tasks.js | 146 ++++++++++++++++++++------- src/02-numbers-tasks.js | 61 ++++++----- src/03-arrays-tasks.js | 50 ++++----- src/04-date-tasks.js | 77 +++++++++++--- src/05-objects-tasks.js | 17 ++-- src/06-promises-tasks.js | 4 +- src/07-conditions-n-loops-tasks.js | 13 ++- src/08-functions-n-closures-tasks.js | 4 +- 8 files changed, 259 insertions(+), 113 deletions(-) diff --git a/src/01-strings-tasks.js b/src/01-strings-tasks.js index afc97fa6b7..b0adaf5442 100644 --- a/src/01-strings-tasks.js +++ b/src/01-strings-tasks.js @@ -5,7 +5,6 @@ * * ******************************************************************************************* */ - /** * Returns the result of concatenation of two strings. * @@ -18,11 +17,10 @@ * 'aa','' => 'aa' * '', 'bb' => 'bb' */ -function concatenateStrings(/* value1, value2 */) { - throw new Error('Not implemented'); +function concatenateStrings(value1, value2) { + return `${value1}${value2}`; } - /** * Returns the length of given string. * @@ -34,8 +32,8 @@ function concatenateStrings(/* value1, value2 */) { * 'b' => 1 * '' => 0 */ -function getStringLength(/* value */) { - throw new Error('Not implemented'); +function getStringLength(value) { + return value.length; } /** @@ -51,8 +49,8 @@ function getStringLength(/* value */) { * 'John','Doe' => 'Hello, John Doe!' * 'Chuck','Norris' => 'Hello, Chuck Norris!' */ -function getStringFromTemplate(/* firstName, lastName */) { - throw new Error('Not implemented'); +function getStringFromTemplate(firstName, lastName) { + return `Hello, ${firstName} ${lastName}!`; } /** @@ -65,11 +63,10 @@ function getStringFromTemplate(/* firstName, lastName */) { * 'Hello, John Doe!' => 'John Doe' * 'Hello, Chuck Norris!' => 'Chuck Norris' */ -function extractNameFromTemplate(/* value */) { - throw new Error('Not implemented'); +function extractNameFromTemplate(value) { + return value.slice(7, -1); } - /** * Returns a first char of the given string. * @@ -80,8 +77,8 @@ function extractNameFromTemplate(/* value */) { * 'John Doe' => 'J' * 'cat' => 'c' */ -function getFirstChar(/* value */) { - throw new Error('Not implemented'); +function getFirstChar(value) { + return value[0]; } /** @@ -95,8 +92,8 @@ function getFirstChar(/* value */) { * 'cat' => 'cat' * '\tHello, World! ' => 'Hello, World!' */ -function removeLeadingAndTrailingWhitespaces(/* value */) { - throw new Error('Not implemented'); +function removeLeadingAndTrailingWhitespaces(value) { + return value.trim(); } /** @@ -110,8 +107,8 @@ function removeLeadingAndTrailingWhitespaces(/* value */) { * 'A', 5 => 'AAAAA' * 'cat', 3 => 'catcatcat' */ -function repeatString(/* value, count */) { - throw new Error('Not implemented'); +function repeatString(value, count) { + return value.repeat(count); } /** @@ -126,8 +123,8 @@ function repeatString(/* value, count */) { * 'I like legends', 'end' => 'I like legs', * 'ABABAB','BA' => 'ABAB' */ -function removeFirstOccurrences(/* str, value */) { - throw new Error('Not implemented'); +function removeFirstOccurrences(str, value) { + return str.replace(value, ''); } /** @@ -141,11 +138,10 @@ function removeFirstOccurrences(/* str, value */) { * '' => 'span' * '' => 'a' */ -function unbracketTag(/* str */) { - throw new Error('Not implemented'); +function unbracketTag(str) { + return str.slice(1, str.length - 1); } - /** * Converts all characters of the specified string into the upper case * @@ -156,8 +152,8 @@ function unbracketTag(/* str */) { * 'Thunderstruck' => 'THUNDERSTRUCK' * 'abcdefghijklmnopqrstuvwxyz' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' */ -function convertToUpperCase(/* str */) { - throw new Error('Not implemented'); +function convertToUpperCase(str) { + return str.toUpperCase(); } /** @@ -175,8 +171,8 @@ function convertToUpperCase(/* str */) { * ], * 'info@gmail.com' => ['info@gmail.com'] */ -function extractEmails(/* str */) { - throw new Error('Not implemented'); +function extractEmails(str) { + return str.split(';'); } /** @@ -202,10 +198,21 @@ function extractEmails(/* str */) { * '└──────────┘\n' * */ -function getRectangleString(/* width, height */) { - throw new Error('Not implemented'); -} +function getRectangleString(width, height) { + let res = ''; + for (let i = 0; i < height; i += 1) { + if (i === 0) { + res += `┌${'─'.repeat(width - 2)}┐\n`; + } else if (i === height - 1) { + res += `└${'─'.repeat(width - 2)}┘\n`; + } else { + res += `│${' '.repeat(width - 2)}│\n`; + } + } + + return res; +} /** * Encode specified string with ROT13 cipher @@ -223,8 +230,20 @@ function getRectangleString(/* width, height */) { * => 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm' * */ -function encodeToRot13(/* str */) { - throw new Error('Not implemented'); +function encodeToRot13(str) { + const charOne = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; + const charTwo = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'; + let result = ''; + let index = 0; + for (let i = 0; i < str.length; i += 1) { + if (str[i] === ' ' || str[i] === '?' || str[i] === '!') { + result += str[i]; + } else { + index = charOne.indexOf(str[i]); + result += charTwo[index]; + } + } + return result; } /** @@ -240,11 +259,10 @@ function encodeToRot13(/* str */) { * isString('test') => true * isString(new String('test')) => true */ -function isString(/* value */) { - throw new Error('Not implemented'); +function isString(value) { + return value instanceof String || typeof value === 'string'; } - /** * Returns playid card id. * @@ -269,10 +287,64 @@ function isString(/* value */) { * 'Q♠' => 50 * 'K♠' => 51 */ -function getCardId(/* value */) { - throw new Error('Not implemented'); -} +function getCardId(value) { + const array = [ + '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 array.indexOf(value); +} module.exports = { concatenateStrings, diff --git a/src/02-numbers-tasks.js b/src/02-numbers-tasks.js index 2dd7964007..2e36a4911b 100644 --- a/src/02-numbers-tasks.js +++ b/src/02-numbers-tasks.js @@ -19,11 +19,10 @@ * 5, 10 => 50 * 5, 5 => 25 */ -function getRectangleArea(/* width, height */) { - throw new Error('Not implemented'); +function getRectangleArea(a, b) { + return a * b; } - /** * Returns a circumference of circle given by radius. * @@ -35,8 +34,8 @@ function getRectangleArea(/* width, height */) { * 3.14 => 19.729201864543903 * 0 => 0 */ -function getCircleCircumference(/* radius */) { - throw new Error('Not implemented'); +function getCircleCircumference(radius) { + return 2 * Math.PI * radius; } /** @@ -51,8 +50,8 @@ function getCircleCircumference(/* radius */) { * 10, 0 => 5 * -3, 3 => 0 */ -function getAverage(/* value1, value2 */) { - throw new Error('Not implemented'); +function getAverage(value1, value2) { + return value1 / 2 + value2 / 2; } /** @@ -70,8 +69,8 @@ function getAverage(/* value1, value2 */) { * (0,0) (1,0) => 1 * (-5,0) (10,-10) => 18.027756377319946 */ -function getDistanceBetweenPoints(/* x1, y1, x2, y2 */) { - throw new Error('Not implemented'); +function getDistanceBetweenPoints(x1, y1, x2, y2) { + return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2); } /** @@ -86,8 +85,8 @@ function getDistanceBetweenPoints(/* x1, y1, x2, y2 */) { * x + 8 = 0 => -8 * 5*x = 0 => 0 */ -function getLinearEquationRoot(/* a, b */) { - throw new Error('Not implemented'); +function getLinearEquationRoot(a, b) { + return -b / a; } @@ -109,8 +108,11 @@ function getLinearEquationRoot(/* a, b */) { * (0,1) (0,1) => 0 * (0,1) (1,2) => 0 */ -function getAngleBetweenVectors(/* x1, y1, x2, y2 */) { - throw new Error('Not implemented'); +function getAngleBetweenVectors(x1, y1, x2, y2) { + const DOT_ONE = x1 * x2 + y1 * y2; + const DOT_TWO = Math.sqrt(x1 * x1 + y1 * y1) * Math.sqrt(x2 * x2 + y2 * y2); + + return Math.acos(DOT_ONE / DOT_TWO); } /** @@ -125,8 +127,8 @@ function getAngleBetweenVectors(/* x1, y1, x2, y2 */) { * 5 => 5 * 0 => 0 */ -function getLastDigit(/* value */) { - throw new Error('Not implemented'); +function getLastDigit(value) { + return value % 10; } @@ -141,8 +143,8 @@ function getLastDigit(/* value */) { * '37' => 37 * '-525.5' => -525.5 */ -function parseNumberFromString(/* value */) { - throw new Error('Not implemented'); +function parseNumberFromString(value) { + return Number(value); } /** @@ -158,8 +160,8 @@ function parseNumberFromString(/* value */) { * 3,3,3 => 5.196152422706632 * 1,2,3 => 3.741657386773941 */ -function getParallelepipedDiagonal(/* a, b, c */) { - throw new Error('Not implemented'); +function getParallelepipedDiagonal(a, b, c) { + return Math.sqrt(a ** 2 + b ** 2 + c ** 2); } @@ -180,8 +182,8 @@ function getParallelepipedDiagonal(/* a, b, c */) { * 1678, 2 => 1700 * 1678, 3 => 2000 */ -function roundToPowerOfTen(/* num, pow */) { - throw new Error('Not implemented'); +function roundToPowerOfTen(num, pow) { + return Math.round(num / 10 ** pow) * 10 ** pow; } /** @@ -201,8 +203,14 @@ function roundToPowerOfTen(/* num, pow */) { * 16 => false * 17 => true */ -function isPrime(/* n */) { - throw new Error('Not implemented'); +function isPrime(n) { + for (let i = 2; i < n - 1; i += 1) { + if (n % i === 0) { + return false; + } + } + + return true; } /** @@ -220,8 +228,11 @@ function isPrime(/* n */) { * toNumber(42, 0) => 42 * toNumber(new Number(42), 0) => 42 */ -function toNumber(/* value, def */) { - throw new Error('Not implemented'); +function toNumber(value, def) { + if (Number(value)) { + return value; + } + return def; } module.exports = { diff --git a/src/03-arrays-tasks.js b/src/03-arrays-tasks.js index d9398fceb7..9ea170abdb 100644 --- a/src/03-arrays-tasks.js +++ b/src/03-arrays-tasks.js @@ -20,8 +20,8 @@ * ['Array', 'Number', 'string'], 'Date' => -1 * [0, 1, 2, 3, 4, 5], 5 => 5 */ -function findElement(/* arr, value */) { - throw new Error('Not implemented'); +function findElement(arr, value) { + return arr.indexOf(value); } /** @@ -35,8 +35,10 @@ function findElement(/* arr, value */) { * 2 => [ 1, 3 ] * 5 => [ 1, 3, 5, 7, 9 ] */ -function generateOdds(/* len */) { - throw new Error('Not implemented'); +function generateOdds(len) { + return Array(len) + .fill(1) + .map((__, index) => 2 * index + 1); } @@ -52,8 +54,8 @@ function generateOdds(/* len */) { * [0, 1, 2, 3, 4, 5] => [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5] * [] => [] */ -function doubleArray(/* arr */) { - throw new Error('Not implemented'); +function doubleArray(arr) { + return arr.concat(arr); } @@ -68,8 +70,8 @@ function doubleArray(/* arr */) { * [-1, 2, -5, -4, 0] => [ 2 ] * [] => [] */ -function getArrayOfPositives(/* arr */) { - throw new Error('Not implemented'); +function getArrayOfPositives(arr) { + return arr.filter((item) => item > 0); } /** @@ -83,8 +85,8 @@ function getArrayOfPositives(/* arr */) { * [ 1, 2, 3, 4, 5 ] => [] * [ 'cat, 'dog', 'raccoon' ] => [ 'cat', 'dog', 'raccoon' ] */ -function getArrayOfStrings(/* arr */) { - throw new Error('Not implemented'); +function getArrayOfStrings(arr) { + return arr.filter((item) => typeof item === 'string'); } /** @@ -100,8 +102,8 @@ function getArrayOfStrings(/* arr */) { * [ 1, 2, 3, 4, 5, 'false' ] => [ 1, 2, 3, 4, 5, 'false' ] * [ false, 0, NaN, '', undefined ] => [ ] */ -function removeFalsyValues(/* arr */) { - throw new Error('Not implemented'); +function removeFalsyValues(arr) { + return arr.filter((item) => Boolean(item) === true); } /** @@ -115,8 +117,8 @@ function removeFalsyValues(/* arr */) { * => [ 'PERMANENT-INTERNSHIP', 'GLUTINOUS-SHRIEK', 'MULTIPLICATIVE-ELEVATION' ], * [ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ] => [ 'A', 'B', 'C', 'D', 'E', 'F', 'G' ] */ -function getUpperCaseStrings(/* arr */) { - throw new Error('Not implemented'); +function getUpperCaseStrings(arr) { + return arr.map((item) => item.toUpperCase()); } @@ -130,8 +132,8 @@ function getUpperCaseStrings(/* arr */) { * [ '', 'a', 'bc', 'def', 'ghij' ] => [ 0, 1, 2, 3, 4 ] * [ 'angular', 'react', 'ember' ] => [ 7, 5, 5 ] */ -function getStringsLength(/* arr */) { - throw new Error('Not implemented'); +function getStringsLength(arr) { + return arr.map((item) => item.length); } /** @@ -145,8 +147,8 @@ function getStringsLength(/* arr */) { * [ 1, 3, 4, 5 ], 2, 1 => [ 1, 2, 3, 4, 5 ] * [ 1, 'b', 'c'], 'x', 0 => [ 'x', 1, 'b', 'c' ] */ -function insertItem(/* arr, item, index */) { - throw new Error('Not implemented'); +function insertItem(arr, item, index) { + return arr.splice(index, 0, item); } /** @@ -159,8 +161,8 @@ function insertItem(/* arr, item, index */) { * [ 1, 3, 4, 5 ], 2 => [ 1, 3 ] * [ 'a', 'b', 'c', 'd'], 3 => [ 'a', 'b', 'c' ] */ -function getHead(/* arr, n */) { - throw new Error('Not implemented'); +function getHead(arr, n) { + return arr.slice(0, n); } @@ -174,8 +176,8 @@ function getHead(/* arr, n */) { * [ 1, 3, 4, 5 ], 2 => [ 4, 5 ] * [ 'a', 'b', 'c', 'd'], 3 => [ 'b', 'c', 'd' ] */ -function getTail(/* arr, n */) { - throw new Error('Not implemented'); +function getTail(arr, n) { + return arr.slice(-n); } @@ -214,8 +216,8 @@ function toCsvText(/* arr */) { * [ 0, 1, 2, 3, 4, 5 ] => [ 0, 1, 4, 9, 16, 25 ] * [ 10, 100, -1 ] => [ 100, 10000, 1 ] */ -function toArrayOfSquares(/* arr */) { - throw new Error('Not implemented'); +function toArrayOfSquares(arr) { + return arr.map((item) => item * item); } diff --git a/src/04-date-tasks.js b/src/04-date-tasks.js index 95ffe2d10d..403dda5665 100644 --- a/src/04-date-tasks.js +++ b/src/04-date-tasks.js @@ -1,3 +1,4 @@ + /* ******************************************************************************************* * * * Please read the following tutorial before implementing tasks: * @@ -6,7 +7,6 @@ * * ******************************************************************************************* */ - /** * Parses a rfc2822 string date representation into date value * For rfc2822 date specification refer to : http://tools.ietf.org/html/rfc2822#page-14 @@ -19,8 +19,8 @@ * 'Tue, 26 Jan 2016 13:48:02 GMT' => Date() * 'Sun, 17 May 1998 03:00:00 GMT+01' => Date() */ -function parseDataFromRfc2822(/* value */) { - throw new Error('Not implemented'); +function parseDataFromRfc2822(value) { + return new Date(value); } /** @@ -34,11 +34,10 @@ function parseDataFromRfc2822(/* value */) { * '2016-01-19T16:07:37+00:00' => Date() * '2016-01-19T08:07:37Z' => Date() */ -function parseDataFromIso8601(/* value */) { - throw new Error('Not implemented'); +function parseDataFromIso8601(value) { + return new Date(value); } - /** * Returns true if specified date is leap year and false otherwise * Please find algorithm here: https://en.wikipedia.org/wiki/Leap_year#Algorithm @@ -53,10 +52,21 @@ function parseDataFromIso8601(/* value */) { * Date(2012,1,1) => true * Date(2015,1,1) => false */ -function isLeapYear(/* date */) { - throw new Error('Not implemented'); -} +function isLeapYear(date) { + const year = date.getFullYear(); + if (year % 4) { + return false; + } + if (year % 100) { + return true; + } + if (year % 400) { + return false; + } + + return true; +} /** * Returns the string representation of the timespan between two dates. @@ -73,10 +83,34 @@ function isLeapYear(/* date */) { * Date(2000,1,1,10,0,0), Date(2000,1,1,10,0,0,250) => "00:00:00.250" * 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'); -} +function timeSpanToString(startDate, endDate) { + const day = endDate.getDay() - startDate.getDay(); + let hours = endDate.getHours() - startDate.getHours(); + let minutes = endDate.getMinutes() - startDate.getMinutes(); + let seconds = endDate.getSeconds() - startDate.getSeconds(); + let milliseconds = endDate.getMilliseconds() - startDate.getMilliseconds(); + if (day >= 1) { + hours += 24; + } + if (hours < 10) { + hours = `0${hours}`; + } + if (minutes < 10) { + minutes = `0${minutes}`; + } + if (seconds < 10) { + seconds = `0${seconds}`; + } + if (milliseconds < 100) { + milliseconds = `0${milliseconds}`; + } + if (milliseconds < 10) { + milliseconds = `0${milliseconds}`; + } + + return `${hours}:${minutes}:${seconds}.${milliseconds}`; +} /** * Returns the angle (in radians) between the hands of an analog clock @@ -94,10 +128,23 @@ function timeSpanToString(/* startDate, endDate */) { * 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'); -} +function angleBetweenClockHands(date) { + let hours = date.getUTCHours(); + const minutes = date.getUTCMinutes(); + if (hours >= 12) { + hours -= 12; + } + const s = minutes / 2; + hours = hours * 30 + s; + + let deg = Math.abs(hours - minutes * 6); + + if (deg > 180) { + deg = 360 - deg; + } + return Math.PI * (deg / 180); +} module.exports = { parseDataFromRfc2822, diff --git a/src/05-objects-tasks.js b/src/05-objects-tasks.js index f26fc4f7a2..149c08e137 100644 --- a/src/05-objects-tasks.js +++ b/src/05-objects-tasks.js @@ -20,8 +20,11 @@ * console.log(r.height); // => 20 * console.log(r.getArea()); // => 200 */ -function Rectangle(/* width, height */) { - throw new Error('Not implemented'); +function Rectangle(width, height) { + this.width = width; + this.height = height; + + this.getArea = () => this.width * this.height; } @@ -35,8 +38,8 @@ function Rectangle(/* width, height */) { * [1,2,3] => '[1,2,3]' * { width: 10, height : 20 } => '{"height":10,"width":20}' */ -function getJSON(/* obj */) { - throw new Error('Not implemented'); +function getJSON(obj) { + return JSON.stringify(obj); } @@ -51,8 +54,10 @@ function getJSON(/* obj */) { * const r = fromJSON(Circle.prototype, '{"radius":10}'); * */ -function fromJSON(/* proto, json */) { - throw new Error('Not implemented'); +function fromJSON(proto, json) { + const obj = JSON.parse(json); + Object.setPrototypeOf(obj, proto); + return obj; } diff --git a/src/06-promises-tasks.js b/src/06-promises-tasks.js index cb6dcd8f52..d920c25b55 100644 --- a/src/06-promises-tasks.js +++ b/src/06-promises-tasks.js @@ -48,8 +48,8 @@ function willYouMarryMe(/* isPositiveAnswer */) { * }) * */ -function processAllPromises(/* array */) { - throw new Error('Not implemented'); +function processAllPromises(array) { + return Promise.all(array); } /** diff --git a/src/07-conditions-n-loops-tasks.js b/src/07-conditions-n-loops-tasks.js index 5807f20fef..622cfb41eb 100644 --- a/src/07-conditions-n-loops-tasks.js +++ b/src/07-conditions-n-loops-tasks.js @@ -27,8 +27,17 @@ * 21 => 'Fizz' * */ -function getFizzBuzz(/* num */) { - throw new Error('Not implemented'); +function getFizzBuzz(num) { + if (num % 3 === 0 && num % 5 === 0) { + return 'FizzBuzz'; + } + if (num % 5 === 0) { + return 'Buzz'; + } + if (num % 3 === 0) { + return 'Fizz'; + } + return num; } diff --git a/src/08-functions-n-closures-tasks.js b/src/08-functions-n-closures-tasks.js index 299a639583..1c433a0aeb 100644 --- a/src/08-functions-n-closures-tasks.js +++ b/src/08-functions-n-closures-tasks.js @@ -23,8 +23,8 @@ * getComposition(Math.sin, Math.asin)(x) => Math.sin(Math.asin(x)) * */ -function getComposition(/* f, g */) { - throw new Error('Not implemented'); +function getComposition(f, g) { + return (x) => f(g(x)); }