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..286dd0cfe 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,10 +69,9 @@ function getStringFromTemplate(firstName, lastName) { * 'Hello, Chuck Norris!' => 'Chuck Norris' */ function extractNameFromTemplate(value) { - throw new Error('Not implemented'); + return value.slice(7, -1); } - /** * Returns a first char of the given string. * @@ -84,7 +83,7 @@ function extractNameFromTemplate(value) { * 'cat' => 'c' */ function getFirstChar(value) { - throw new Error('Not implemented'); + return value[0]; } /** @@ -99,7 +98,7 @@ function getFirstChar(value) { * '\tHello, World! ' => 'Hello, World!' */ function removeLeadingAndTrailingWhitespaces(value) { - throw new Error('Not implemented'); + return value.trim(); } /** @@ -114,7 +113,7 @@ function removeLeadingAndTrailingWhitespaces(value) { * 'cat', 3 => 'catcatcat' */ function repeatString(value, count) { - throw new Error('Not implemented'); + return value.repeat(count); } /** @@ -130,7 +129,7 @@ function repeatString(value, count) { * 'ABABAB','BA' => 'ABAB' */ function removeFirstOccurrences(str, value) { - throw new Error('Not implemented'); + return str.replace(value, ''); } /** @@ -145,7 +144,7 @@ function removeFirstOccurrences(str, value) { * '' => 'a' */ function unbracketTag(str) { - throw new Error('Not implemented'); + return str.slice(1, str.length-1); } @@ -160,7 +159,7 @@ function unbracketTag(str) { * 'abcdefghijklmnopqrstuvwxyz' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' */ function convertToUpperCase(str) { - throw new Error('Not implemented'); + return str.toUpperCase(); } /** @@ -174,7 +173,7 @@ function convertToUpperCase(str) { * 'info@gmail.com' => ['info@gmail.com'] */ function extractEmails(str) { - throw new Error('Not implemented'); + return str.split(';'); } /** @@ -201,7 +200,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 +222,11 @@ function getRectangleString(width, height) { * */ function encodeToRot13(str) { - throw new Error('Not implemented'); + var input = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; + var output = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'; + var index = x => input.indexOf(x); + var translate = x => index(x) > -1 ? output[index(x)] : x; + return str.split('').map(translate).join(''); } /** @@ -238,7 +243,7 @@ function encodeToRot13(str) { * isString(new String('test')) => true */ function isString(value) { - throw new Error('Not implemented'); + return typeof value === 'string' || value instanceof String; } @@ -267,7 +272,14 @@ function isString(value) { * 'K♠' => 51 */ function getCardId(value) { - throw new Error('Not implemented'); + const cards = [ + '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 cards.indexOf(value); } diff --git a/task/02-numbers-tasks.js b/task/02-numbers-tasks.js index c9ed20208..dc91648f2 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.hypot((x2 - x1), (y2 - y1)); } /** @@ -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,9 @@ 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(x1 * x1 + y1 * y1) * Math.sqrt(x2 * x2 + y2 * y2)) + ); } /** @@ -127,7 +129,7 @@ function getAngleBetweenVectors(x1, y1, x2, y2) { * 0 => 0 */ function getLastDigit(value) { - throw new Error('Not implemented'); + return value % 10; } @@ -143,7 +145,7 @@ function getLastDigit(value) { * '-525.5' => -525.5 */ function parseNumberFromString(value) { - throw new Error('Not implemented'); + return Number.parseFloat(value); } /** @@ -160,7 +162,7 @@ function parseNumberFromString(value) { * 1,2,3 => 3.741657386773941 */ function getParallelipidedDiagonal(a,b,c) { - throw new Error('Not implemented'); + return Math.sqrt(a * a + b * b + c * c); } /** @@ -169,7 +171,7 @@ function getParallelipidedDiagonal(a,b,c) { * @param {number} num * @param {number} pow * @return {number} - * + * * @example: * 1234, 0 => 1234 * 1234, 1 => 1230 @@ -181,7 +183,7 @@ function getParallelipidedDiagonal(a,b,c) { * 1678, 3 => 2000 */ function roundToPowerOfTen(num, pow) { - throw new Error('Not implemented'); + return Math.round(num / Math.pow(10, pow)) * Math.pow(10, pow); } /** @@ -190,7 +192,7 @@ function roundToPowerOfTen(num, pow) { * * @param {number} n * @return {bool} - * + * * @example: * 4 => false * 5 => true @@ -202,7 +204,22 @@ function roundToPowerOfTen(num, pow) { * 17 => true */ function isPrime(n) { - throw new Error('Not implemented'); + if (n <= 1) { + return false; + } else if (n <= 3) { + return true; + } else if (!(n % 2) || !(n % 3)) { + return false; + } + + let i = 5; + while (i * i <= n) { + if (!(n % i) || !(n % (i + 2))) { + return false; + } + i += 6; + } + return true; } /** @@ -221,7 +238,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..eaf778a51 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 new Date(value); } /** @@ -37,7 +37,7 @@ function parseDataFromRfc2822(value) { * '2016-01-19T08:07:37Z' => Date() */ function parseDataFromIso8601(value) { - throw new Error('Not implemented'); + return new Date(value); } @@ -56,7 +56,9 @@ function parseDataFromIso8601(value) { * Date(2015,1,1) => false */ function isLeapYear(date) { - throw new Error('Not implemented'); + if ((date.getFullYear() % 4 == 0 && (date.getFullYear() % 100 != 0)) || date.getFullYear() % 400 == 0) + return true; + return false; } @@ -76,14 +78,14 @@ 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'); + return (new Date(endDate - startDate)).toISOString().slice(11, -1); } /** * Returns the angle (in radians) between the hands of an analog clock for the specified Greenwich time. * If you have problem with solution please read: https://en.wikipedia.org/wiki/Clock_angle_problem - * + * * @param {date} date * @return {number} * @@ -94,7 +96,14 @@ function timeSpanToString(startDate, endDate) { * Date.UTC(2016,3,5,21, 0) => Math.PI/2 */ function angleBetweenClockHands(date) { - throw new Error('Not implemented'); + let h = new Date(date).getUTCHours(); + h = h % 12; + let min = new Date(date).getUTCMinutes(); + let angle = Math.abs(1/2 * (60 * h - 11 * min)); + if (angle > 180) { + angle = (360 - angle); + } + return angle / 180 * Math.PI; } diff --git a/task/04-arrays-tasks.js b/task/04-arrays-tasks.js index ff3a4c019..75a9bbb7e 100644 --- a/task/04-arrays-tasks.js +++ b/task/04-arrays-tasks.js @@ -9,257 +9,282 @@ * * *********************************************************************************************/ - + /** * Returns an index of the specified element in array or -1 if element is not found - * + * * @param {array} arr * @param {any} value * @return {number} - * + * * @example - * ['Ace', 10, true], 10 => 1 - * ['Array', 'Number', 'string'], 'Date' => -1 + * ['Ace', 10, true], 10 => 1 + * ['Array', 'Number', 'string'], 'Date' => -1 * [0, 1, 2, 3, 4, 5], 5 => 5 */ function findElement(arr, value) { - throw new Error('Not implemented'); + return arr.indexOf(value); } /** * Generates an array of odd numbers of the specified length - * + * * @param {number} len * @return {array} - * + * * @example - * 1 => [ 1 ] - * 2 => [ 1, 3 ] + * 1 => [ 1 ] + * 2 => [ 1, 3 ] * 5 => [ 1, 3, 5, 7, 9 ] */ function generateOdds(len) { - throw new Error('Not implemented'); + let arr = new Array(len); + arr.fill(1); + return arr.map((elem, index) => + { + return elem = index*2 + 1; + }); } /** * Returns the doubled array - elements of the specified array are repeated twice using original order - * + * * @param {array} arr * @return {array} - * + * * @example - * ['Ace', 10, true] => ['Ace', 10, true, 'Ace', 10, true] + * ['Ace', 10, true] => ['Ace', 10, true, 'Ace', 10, true] * [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'); + arr.map(elem => + { + return arr.push(elem); + }); + return arr; } /** * Returns an array of positive numbers from the specified array in original order - * + * * @param {array} arr * @return {array} - * + * * @example * [ 0, 1, 2, 3, 4, 5 ] => [ 1, 2, 3, 4, 5 ] * [-1, 2, -5, -4, 0] => [ 2 ] - * [] => [] + * [] => [] */ function getArrayOfPositives(arr) { - throw new Error('Not implemented'); + return arr.filter((item, i, arr) => item > 0); } /** * Returns the array with strings only in the specified array (in original order) - * + * * @param {array} arr * @return {array} - * + * * @example * [ 0, 1, 'cat', 3, true, 'dog' ] => [ 'cat', 'dog' ] * [ 1, 2, 3, 4, 5 ] => [] * [ 'cat, 'dog', 'raccon' ] => [ 'cat', 'dog', 'racoon' ] */ function getArrayOfStrings(arr) { - throw new Error('Not implemented'); + return arr.filter(elem => typeof elem === 'string' || elem instanceof String); } /** * Removes falsy values from the specified array * Falsy values: false, null, 0, "", undefined, and NaN. * (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean#Description) - * + * * @param {array} arr * @return {array} - * + * * @example * [ 0, false, 'cat', NaN, true, '' ] => [ 'cat', true ] * [ 1, 2, 3, 4, 5, 'false' ] => [ 1, 2, 3, 4, 5, 'false' ] * [ false, 0, NaN, '', undefined ] => [ ] */ function removeFalsyValues(arr) { - throw new Error('Not implemented'); + return arr.filter((item, i, arr) => !!item); } /** * Returns the array of useprcase strings from the specified array - * + * * @param {array} arr * @return {array} - * + * * @example * [ 'permanent-internship', 'glutinous-shriek', 'multiplicative-elevation' ] => [ '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'); + return arr.map(elem => + { + return elem.toUpperCase(); + }); } /** * Returns the array of string lengths from the specified string array. - * + * * @param {array} arr * @return {array} - * + * * @example * [ '', 'a', 'bc', 'def', 'ghij' ] => [ 0, 1, 2, 3, 4 ] * [ 'angular', 'react', 'ember' ] => [ 7, 5, 5 ] */ function getStringsLength(arr) { - throw new Error('Not implemented'); + return arr.map((item, i, arr) => item.length); } /** * Inserts the item into specified array at specified index - * + * * @param {array} arr * @param {any} item - * @param {number} index - * + * @param {number} index + * * @example * [ 1, 3, 4, 5 ], 2, 1 => [ 1, 2, 3, 4, 5 ] * [ 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); } /** * Returns the n first items of the specified array - * + * * @param {array} arr - * @param {number} n - * + * @param {number} n + * * @example * [ 1, 3, 4, 5 ], 2 => [ 1, 2 ] * [ 'a', 'b', 'c', 'd'], 3 => [ 'a', 'b', 'c' ] */ function getHead(arr, n) { - throw new Error('Not implemented'); + return arr.slice(0, n); } /** * Returns the n last items of the specified array - * + * * @param {array} arr - * @param {number} n - * + * @param {number} n + * * @example * [ 1, 3, 4, 5 ], 2 => [ 4, 5 ] * [ 'a', 'b', 'c', 'd'], 3 => [ 'b', 'c', 'd' ] */ function getTail(arr, n) { - throw new Error('Not implemented'); + return arr.splice( -n); } /** * Returns CSV represebtation of two-dimentional numeric array. * https://en.wikipedia.org/wiki/Comma-separated_values - * + * * @param {array} arr * @return {string} - * + * * @example * [ * [ 0, 1, 2, 3, 4 ], * [ 10,11,12,13,14 ], * [ 20,21,22,23,24 ], * [ 30,31,32,33,34 ] - * ] - * => + * ] + * => * '0,1,2,3,4\n' * +'10,11,12,13,14\n' * +'20,21,22,23,24\n' * +'30,31,32,33,34' */ function toCsvText(arr) { - throw new Error('Not implemented'); + let arrStr = arr.map((elem, index) => + { + if (index != arr.length-1) + return elem.join(',') + '\n'; + else + return elem.join(','); + }).join(''); + return arrStr; } /** * Transforms the numeric array into the according array of squares: * f(x) = x * x - * + * * @param {array} arr * @return {array} - * + * * @example * [ 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'); + return arr.map((item, i, arr) => item ** 2); } /** * Transforms the numeric array to the according moving sum array: - * f[n] = x[0] + x[1] + x[2] +...+ x[n] + * f[n] = x[0] + x[1] + x[2] +...+ x[n] * or f[n] = f[n-1] + x[n] - * + * * @param {array} arr * @return {array} - * + * * Example : * [ 1, 1, 1, 1, 1 ] => [ 1, 2, 3, 4, 5 ] * [ 10, -10, 10, -10, 10 ] => [ 10, 0, 10, 0, 10 ] - * [ 0, 0, 0, 0, 0] => [ 0, 0, 0, 0, 0] + * [ 0, 0, 0, 0, 0] => [ 0, 0, 0, 0, 0] * [ 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 sum = 0; + return arr.map((elem) => + { + elem += sum; + sum = elem; + return elem; + }); } /** * Returns every second item from the specified array: - * + * * @param {array} arr * @return {array} - * + * * Example : * [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] => [ 2, 4, 6, 8, 10 ] * [ 'a', 'b', 'c' , null ] => [ "b", null ] * [ "a" ] => [] */ function getSecondItems(arr) { - throw new Error('Not implemented'); + return arr.filter((elem, index) => index % 2); } /** * Propagates every item in sequence its position times - * Returns an array that consists of: one first item, two second items, tree third items etc. - * - * @param {array} arr + * Returns an array that consists of: one first item, two second items, tree third items etc. + * + * @param {array} arr * @return {array} - * + * * @example : * [] => [] * [ 1 ] => [ 1 ] @@ -268,13 +293,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'); + return arr.reduce((acc, elem, index) => acc.concat( + Array.from({length: index + 1}, () => elem)), [] + ); } -/** +/** * Returns the 3 largest numbers from the specified array - * + * * @param {array} arr * @return {array} * @@ -286,16 +313,16 @@ function propagateItemsByPositionIndex(arr) { * [ 10, 10, 10, 10 ] => [ 10, 10, 10 ] */ function get3TopItems(arr) { - throw new Error('Not implemented'); + return arr.sort((a, b) => b - a).slice(0, 3); } - - -/** + + +/** * Returns the number of positive numbers from specified array - * + * * @param {array} arr * @return {number} - * + * * @example * [ ] => 0 * [ -1, 0, 1 ] => 1 @@ -306,13 +333,13 @@ function get3TopItems(arr) { function getPositivesCount(arr) { throw new Error('Not implemented'); } - -/** + +/** * Sorts digit names - * + * * @param {array} arr * @return {array} - * + * * @example * [] => [] * [ 'nine','one' ] => [ 'one', 'nine' ] @@ -321,15 +348,17 @@ function getPositivesCount(arr) { * [ 'one','one','one','zero' ] => [ 'zero','one','one','one' ] */ function sortDigitNamesByNumericOrder(arr) { - throw new Error('Not implemented'); + const mapped1 = { 0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine' }; + const mapped2 = { 'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9 }; + return arr.map(x => mapped2[x]).sort().map(x => mapped1[x]); } -/** +/** * Returns the sum of all items in the specified array of numbers - * + * * @param {array} arr * @return {number} - * + * * @example * [] => 0 * [ 1, 2, 3 ] => 6 @@ -337,15 +366,15 @@ function sortDigitNamesByNumericOrder(arr) { * [ 1, 10, 100, 1000 ] => 1111 */ function getItemsSum(arr) { - throw new Error('Not implemented'); + return arr.reduce((acc, elem) => acc + elem, 0); } - -/** + +/** * Returns the number of all falsy value in the specified array - * + * * @param {array} arr * @return {array} - * + * * @example * [] => 0 * [ 1, '', 3 ] => 1 @@ -353,49 +382,61 @@ function getItemsSum(arr) { * [ null, undefined, NaN, false, 0, '' ] => 6 */ function getFalsyValuesCount(arr) { - throw new Error('Not implemented'); + let sum = 0; + arr.map(elem => + { + if (Boolean(elem) == 0) + return sum++; + }); + return sum; } /** - * Returns a number of all occurences of the specified item in an array - * + * Returns a number of all occurences of the specified item in an array + * * @param {array} arr - * @param {any} item + * @param {any} item * @return {number} - * + * * @example * [ 0, 0, 1, 1, 1, 2 ], 1 => 3 * [ 1, 2, 3, 4, 5 ], 0 => 0 * [ 'a','b','c','c' ], 'c'=> 2 - * [ null, undefined, null ], null => 2 + * [ null, undefined, null ], null => 2 * [ true, 0, 1, 'true' ], true => 1 */ function findAllOccurences(arr, item) { - throw new Error('Not implemented'); + let num = 0; + arr.map(elem => + { + if (elem === item) + return num++; + }) + return num; } /** - * Concatenates all elements from specified array into single string with ',' delimeter - * - * @param {array} arr + * Concatenates all elements from specified array into single string with ',' delimeter + * + * @param {array} arr * @return {string} - * + * * @example * [0, false, 'cat', NaN, true, ''] => '0,false,cat,NaN,true,' * [1, 2, 3, 4, 5] => '1,2,3,4,5' * ['rock', 'paper', 'scissors'] => 'rock,paper,scissors' */ function toStringList(arr) { - throw new Error('Not implemented'); + return arr.join(); } /** * Sorts the specified array by country name first and city name (if countries are equal) in ascending order. - * + * * @param {array} arr * @return {array} - * + * * @example * [ * { country: 'Russia', city: 'Moscow' }, @@ -404,7 +445,7 @@ function toStringList(arr) { * { country: 'Russia', city: 'Saint Petersburg' }, * { country: 'Poland', city: 'Krakow' }, * { country: 'Belarus', city: 'Brest' } - * ] + * ] * => * [ * { country: 'Belarus', city: 'Brest' }, @@ -415,38 +456,61 @@ function toStringList(arr) { * { country: 'Russia', city: 'Saint Petersburg' } */ function sortCitiesArray(arr) { - throw new Error('Not implemented'); + function compareLocation(a, b) { + if (a.country < b.country) { + return -1; + } + if (a.country > b.country) { + return 1; + } + if (a.country === b.country) { + if (a.city < b.city) { + return -1; + } + if (a.city > b.city) { + return 1; + } + if (a.city === b.city) { + return 0; + } + } + } + return arr.sort(compareLocation); } /** * Creates an indentity matrix of the specified size - * + * * @param {number} n * @return {array} - * + * * @example * 1 => [[1]] - * + * * 2 => [[1,0], * [0,1]] - * + * * [[1,0,0,0,0], * [0,1,0,0,0], * 5 => [0,0,1,0,0], * [0,0,0,1,0], - * [0,0,0,0,1]] + * [0,0,0,0,1]] */ function getIdentityMatrix(n) { - throw new Error('Not implemented'); + return Array.from({length: n}, function (elem, index) { + let row = new Array(n).fill(0, 0, n); + row[index] = 1; + return row; + }); } /** * Creates an array of integers from the specified start to end (inclusive) - * + * * @param {number} start * @param {number} end * @return {array} - * + * * @example * 1, 5 => [ 1, 2, 3, 4, 5 ] * -2, 2 => [ -2, -1, 0, 1, 2 ] @@ -454,7 +518,7 @@ function getIdentityMatrix(n) { * 3, 3 => [ 3 ] */ function getIntervalArray(start, end) { - throw new Error('Not implemented'); + return Array.from({length: end - start + 1}, (elem, index) => index + start); } /** @@ -462,14 +526,21 @@ function getIntervalArray(start, end) { * * @param {array} arr * @return {array} - * + * * @example * [ 1, 2, 3, 3, 2, 1 ] => [ 1, 2, 3 ] * [ 'a', 'a', 'a', 'a' ] => [ 'a' ] * [ 1, 1, 2, 2, 3, 3, 4, 4] => [ 1, 2, 3, 4] */ function distinct(arr) { - throw new Error('Not implemented'); + let resArr = []; + arr.map(elem => + { + if (resArr.indexOf(elem) == -1) + resArr.push(elem); + return resArr; + }); + return resArr; } /** @@ -491,19 +562,27 @@ function distinct(arr) { * { country: 'Belarus', city: 'Grodno' }, * { country: 'Belarus', city: 'Minsk' }, * { country: 'Poland', city: 'Lodz' } - * ], - * item => item.country, + * ], + * item => item.country, * item => item.city * ) - * => + * => * Map { * "Belarus" => ["Brest", "Grodno", "Minsk"], - * "Russia" => ["Omsk", "Samara"], + * "Russia" => ["Omsk", "Samara"], * "Poland" => ["Lodz"] * } */ function group(array, keySelector, valueSelector) { - throw new Error('Not implemented'); + let _map = new Map(); + array.map((x, ind) => + { + if( _map.has(keySelector(x)) ) + _map.get(keySelector(x)).push(valueSelector(x)); + else + _map.set(keySelector(x), [valueSelector(x)]); + }); + return _map; } @@ -513,13 +592,18 @@ function group(array, keySelector, valueSelector) { * @param {array} arr * @param {Function} childrenSelector, a transform function to apply to each element that returns an array of children * @return {array} - * + * * @example * [[1, 2], [3, 4], [5, 6]], (x) => x => [ 1, 2, 3, 4, 5, 6 ] * ['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 myArr = []; + arr.map(elem => + { + myArr.push(...childrenSelector(elem)); + }); + return myArr; } @@ -529,70 +613,80 @@ function selectMany(arr, childrenSelector) { * @param {array} arr * @param {array} indexes * @return {any} element from array - * + * * @example - * [[1, 2], [3, 4], [5, 6]], [0,0] => 1 (arr[0][0]) - * ['one','two','three'], [2] => 'three' (arr[2]) + * [[1, 2], [3, 4], [5, 6]], [0,0] => 1 (arr[0][0]) + * ['one','two','three'], [2] => 'three' (arr[2]) * [[[ 1, 2, 3]]], [ 0, 0, 1 ] => 2 (arr[0][0][1]) */ function getElementByIndexes(arr, indexes) { - throw new Error('Not implemented'); + return indexes.map(elem => arr = arr[elem])[indexes.length - 1]; } /** * Swaps the head and tail of the specified array: - * the head (first half) of array move to the end, the tail (last half) move to the start. + * the head (first half) of array move to the end, the tail (last half) move to the start. * The middle element (if exists) leave on the same position. - * - * + * + * * @param {array} arr * @return {array} - * + * * @example * [ 1, 2, 3, 4, 5 ] => [ 4, 5, 3, 1, 2 ] - * \----/ \----/ - * head tail + * \----/ \----/ + * head tail + * + * [ 1, 2 ] => [ 2, 1 ] + * [ 1, 2, 3, 4, 5, 6, 7, 8 ] => [ 5, 6, 7, 8, 1, 2, 3, 4 ] * - * [ 1, 2 ] => [ 2, 1 ] - * [ 1, 2, 3, 4, 5, 6, 7, 8 ] => [ 5, 6, 7, 8, 1, 2, 3, 4 ] - * */ function swapHeadAndTail(arr) { - throw new Error('Not implemented'); + let len = Math.floor(arr.length / 2); + let head = arr.splice(0, len); + let tail = arr.splice(arr.length-len, len); + let array = []; + array.push(...tail); + if(arr.length % 2) { + let middle = arr.splice(0, 1); + array.push(...middle) + } + array.push(...head); + return array; } module.exports = { - findElement: findElement, - generateOdds: generateOdds, - doubleArray: doubleArray, - getArrayOfPositives: getArrayOfPositives, - getArrayOfStrings: getArrayOfStrings, - removeFalsyValues: removeFalsyValues, - getUpperCaseStrings: getUpperCaseStrings, - getStringsLength: getStringsLength, - insertItem: insertItem, - getHead: getHead, - getTail: getTail, - toCsvText: toCsvText, - toStringList: toStringList, - toArrayOfSquares: toArrayOfSquares, - getMovingSum: getMovingSum, - getSecondItems: getSecondItems, - propagateItemsByPositionIndex: propagateItemsByPositionIndex, - get3TopItems: get3TopItems, - getPositivesCount: getPositivesCount, - sortDigitNamesByNumericOrder: sortDigitNamesByNumericOrder, - getItemsSum: getItemsSum, - getFalsyValuesCount: getFalsyValuesCount, - findAllOccurences: findAllOccurences, - sortCitiesArray: sortCitiesArray, - getIdentityMatrix: getIdentityMatrix, - getIntervalArray: getIntervalArray, - distinct: distinct, - group: group, - selectMany: selectMany, - getElementByIndexes: getElementByIndexes, - swapHeadAndTail: swapHeadAndTail + findElement: findElement, + generateOdds: generateOdds, + doubleArray: doubleArray, + getArrayOfPositives: getArrayOfPositives, + getArrayOfStrings: getArrayOfStrings, + removeFalsyValues: removeFalsyValues, + getUpperCaseStrings: getUpperCaseStrings, + getStringsLength: getStringsLength, + insertItem: insertItem, + getHead: getHead, + getTail: getTail, + toCsvText: toCsvText, + toStringList: toStringList, + toArrayOfSquares: toArrayOfSquares, + getMovingSum: getMovingSum, + getSecondItems: getSecondItems, + propagateItemsByPositionIndex: propagateItemsByPositionIndex, + get3TopItems: get3TopItems, + getPositivesCount: getPositivesCount, + sortDigitNamesByNumericOrder: sortDigitNamesByNumericOrder, + getItemsSum: getItemsSum, + getFalsyValuesCount: getFalsyValuesCount, + findAllOccurences: findAllOccurences, + sortCitiesArray: sortCitiesArray, + getIdentityMatrix: getIdentityMatrix, + getIntervalArray: getIntervalArray, + distinct: distinct, + group: group, + selectMany: selectMany, + getElementByIndexes: getElementByIndexes, + swapHeadAndTail: swapHeadAndTail }; diff --git a/task/05-regex-tasks.js b/task/05-regex-tasks.js index b1c60f2d4..ddc8254f4 100644 --- a/task/05-regex-tasks.js +++ b/task/05-regex-tasks.js @@ -31,7 +31,7 @@ * @return {RegExp} */ function getRegexForGuid() { - throw new Error('Not implemented'); + return /{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}}/i } @@ -53,7 +53,7 @@ function getRegexForGuid() { * */ function getRegexForPitSpot() { - throw new Error('Not implemented'); + return /^(pi|s|r).+$/; } @@ -72,7 +72,7 @@ function getRegexForPitSpot() { * @return {RegExp} */ function getRegexForIPv4() { - throw new Error('Not implemented'); + return new RegExp(/^([0-9]|[0-9][0-9]|[0-1][0-9][0-9]|2[0-4][0-9]|25[0-4])(\.([0-9]|[0-9][0-9]|[0-1][0-9][0-9]|2[0-4][0-9]|25[0-4])){3}$/); } @@ -91,7 +91,7 @@ function getRegexForIPv4() { * @return {RegExp} */ function getRegexForSSN() { - throw new Error('Not implemented'); + return /^(?!0{3})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$/; } @@ -116,7 +116,9 @@ function getRegexForSSN() { * 'Pa55'.match(validator) => false */ function getPasswordValidator(minLength) { - throw new Error('Not implemented'); + return new RegExp( + `^(?=[0-9A-Za-z]{${minLength},})(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])` + ); } diff --git a/task/06-conditions-n-loops-tasks.js b/task/06-conditions-n-loops-tasks.js index 249194c34..92018550b 100644 --- a/task/06-conditions-n-loops-tasks.js +++ b/task/06-conditions-n-loops-tasks.js @@ -30,7 +30,14 @@ * */ function getFizzBuzz(num) { - throw new Error('Not implemented'); + if((num % 3 === 0) && (num % 5 === 0)) { + return 'FizzBuzz' + } else if(num % 5 === 0) { + return'Buzz' + } else if (num % 3 === 0) { + return 'Fizz' + } + return num } @@ -46,7 +53,7 @@ function getFizzBuzz(num) { * 10 => 3628800 */ function getFactorial(n) { - throw new Error('Not implemented'); + return n < 1 ? 1 : n * getFactorial(n - 1); } @@ -63,7 +70,11 @@ function getFactorial(n) { * -1,1 => 0 ( = -1 + 0 + 1 ) */ function getSumBetweenNumbers(n1, n2) { - throw new Error('Not implemented'); + let count = 0; + for(let i = n1; i <=n2; i++ ) { + count +=i + } + return count; } @@ -82,30 +93,30 @@ function getSumBetweenNumbers(n1, n2) { * 10,10,10 => true */ function isTriangle(a,b,c) { - throw new Error('Not implemented'); + return (a + b > c) && (a + c > b) && (b + c > a); } /** * Returns true, if two specified axis-aligned rectangles overlap, otherwise false. - * Each rectangle representing by object + * Each rectangle representing by object * { * top: 5, * left: 5, * width: 20, * height: 10 * } - * + * * (5;5) - * ------------- - * | | + * ------------- + * | | * | | height = 10 - * ------------- - * width=20 - * + * ------------- + * width=20 + * * NOTE: Please use canvas coordinate space (https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_shapes#The_grid), * it differs from Cartesian coordinate system. - * + * * @param {object} rect1 * @param {object} rect2 * @return {bool} @@ -113,33 +124,38 @@ function isTriangle(a,b,c) { * @example: * { top: 0, left: 0, width: 10, height: 10 }, * { top: 5, left: 5, width: 20, height: 20 } => true - * + * * { top: 0, left: 0, width: 10, height: 10 }, * { top:20, left:20, width: 20, height: 20 } => false - * + * */ function doRectanglesOverlap(rect1, rect2) { - throw new Error('Not implemented'); + return !( + rect1.left + rect1.width < rect2.left || + rect2.left + rect2.width < rect1.left || + rect1.top + rect1.height < rect2.top || + rect2.top + rect2.height < rect1.top + ); } /** * Returns true, if point lies inside the circle, otherwise false. - * Circle is an object of + * Circle is an object of * { * center: { - * x: 5, + * x: 5, * y: 5 - * }, + * }, * radius: 20 * } - * - * Point is object of + * + * Point is object of * { * x: 5, * y: 5 * } - * + * * @param {object} circle * @param {object} point * @return {bool} @@ -147,10 +163,12 @@ function doRectanglesOverlap(rect1, rect2) { * @example: * { center: { x:0, y:0 }, radius:10 }, { x:0, y:0 } => true * { center: { x:0, y:0 }, radius:10 }, { x:10, y:10 } => false - * + * */ function isInsideCircle(circle, point) { - throw new Error('Not implemented'); + let dx = point.x - circle.center.x; + let dy = point.y - circle.center.y; + return (dx*dx + dy*dy) < (circle.radius * circle.radius); } @@ -166,7 +184,13 @@ function isInsideCircle(circle, point) { * 'entente' => null */ function findFirstSingleChar(str) { - throw new Error('Not implemented'); + for (let i = 0; i < str.length; i++) { + if (str.indexOf(str[i]) == str.lastIndexOf(str[i])) { + return str[i]; + } + } + + return null; } @@ -192,7 +216,11 @@ function findFirstSingleChar(str) { * */ function getIntervalString(a, b, isStartIncluded, isEndIncluded) { - throw new Error('Not implemented'); + let res = ""; + res += (isStartIncluded && res.length === 0) ? "[" : "("; + res += a <= b ? `${a}, ${b}` : `${b}, ${a}`; + res += isEndIncluded ? "]" : ")"; + return res; } @@ -209,7 +237,7 @@ function getIntervalString(a, b, isStartIncluded, isEndIncluded) { * 'noon' => 'noon' */ function reverseString(str) { - throw new Error('Not implemented'); + return str.split('').reverse().join(''); } @@ -226,7 +254,7 @@ function reverseString(str) { * 34143 => 34143 */ function reverseInteger(num) { - throw new Error('Not implemented'); + return Number.parseInt(reverseString(num.toString())); } @@ -251,7 +279,17 @@ function reverseInteger(num) { * 4916123456789012 => false */ function isCreditCardNumber(ccn) { - throw new Error('Not implemented'); + ccn = [...String(ccn)].reverse(); + ccn = ccn.reduce(function(sum, val, ind) + { + let dig = Number(val); + if(ind % 2) + dig *= 2; + sum += Math.floor(dig / 10); + sum += dig % 10; + return sum; + }, 0); + return (ccn * 3) % 10 == 0; } @@ -270,7 +308,13 @@ function isCreditCardNumber(ccn) { * 165536 (1+6+5+5+3+6 = 26, 2+6 = 8) => 8 */ function getDigitalRoot(num) { - throw new Error('Not implemented'); + const getDigitSum = (num) => num.toString().split('').map(Number).reduce((acc, elem) => acc + elem); + + do { + num = getDigitSum(num); + } while (num > 9); + + return num; } @@ -293,10 +337,29 @@ function getDigitalRoot(num) { * '[[][][[]]]' => true * '[[][]][' => false * '{)' = false - * '{[(<{[]}>)]}' = true + * '{[(<{[]}>)]}' = true */ function isBracketsBalanced(str) { - throw new Error('Not implemented'); + let pair = { + '>': '<', + ')': '(', + ']': '[', + '}': '{' + } + let res = [...str].reduce(function(acc, x, ind) + { + if (['(', '{', '[', '<'].indexOf(x) != -1) + acc.push(x); + else + { + if (acc.length > 0 && acc[acc.length - 1] == pair[x]) + acc.pop(); + else + acc.push(x); + } + return acc; + }, []); + return res.length == 0; } diff --git a/task/07-yield-tasks.js b/task/07-yield-tasks.js index a2369790a..f975baff5 100644 --- a/task/07-yield-tasks.js +++ b/task/07-yield-tasks.js @@ -33,7 +33,19 @@ * */ function* get99BottlesOfBeer() { - throw new Error('Not implemented'); + for (let bottles = 99; bottles > 2; bottles--) { + yield `${bottles} bottles of beer on the wall, ${bottles} bottles of beer.`; + yield `Take one down and pass it around, ${bottles - 1} bottles of beer on the wall.`; + } + + yield `2 bottles of beer on the wall, 2 bottles of beer.`; + yield `Take one down and pass it around, 1 bottle of beer on the wall.`; + + yield `1 bottle of beer on the wall, 1 bottle of beer.`; + yield `Take one down and pass it around, no more bottles of beer on the wall.`; + + yield 'No more bottles of beer on the wall, no more bottles of beer.'; + yield 'Go to the store and buy some more, 99 bottles of beer on the wall.'; } @@ -47,7 +59,19 @@ function* get99BottlesOfBeer() { * */ function* getFibonacciSequence() { - throw new Error('Not implemented'); + yield 0; + yield 1; + + let last = 0; + let current = 1; + let new_val = 1; + + while (true){ + yield new_val; + last = current; + current = new_val; + new_val = last + current; + } } @@ -82,7 +106,14 @@ function* getFibonacciSequence() { * */ function* depthTraversalTree(root) { - throw new Error('Not implemented'); + let q1 = [root]; + while (q1.length) { + let node = q1.pop(); + yield node; + if (node.children) { + q1.push(...node.children.reverse()); + } + } } @@ -108,7 +139,24 @@ function* depthTraversalTree(root) { * */ function* breadthTraversalTree(root) { - throw new Error('Not implemented'); + let queue = [root]; + + while (queue.length != 0) { + let node = queue.shift(); + yield node; + + if (node.children !== undefined) { + + for (let child of node.children) { + + if (queue.length == 0 && child.children === undefined) { + yield child; + } else { + queue.push(child); + } + } + } + } } @@ -126,7 +174,18 @@ function* breadthTraversalTree(root) { * [ 1, 3, 5, ... ], [ -1 ] => [ -1, 1, 3, 5, ...] */ function* mergeSortedSequences(source1, source2) { - throw new Error('Not implemented'); + const sources = [source1(), source2()]; + let it = [sources[0].next(), sources[1].next()]; + + while (true) { + if (it[0].value >= it[1].value || it[0].value === undefined) { + yield it[1].value; + it[1] = sources[1].next(); + } else { + yield it[0].value; + it[0] = sources[0].next(); + } + } } diff --git a/task/08-objects-tasks.js b/task/08-objects-tasks.js index 610b1e7b2..c06c91c2f 100644 --- a/task/08-objects-tasks.js +++ b/task/08-objects-tasks.js @@ -23,7 +23,12 @@ * console.log(r.getArea()); // => 200 */ function Rectangle(width, height) { - throw new Error('Not implemented'); + this.width = width; + this.height = height; + + this.getArea = () => { + return this.width * this.height + } } @@ -38,7 +43,7 @@ function Rectangle(width, height) { * { width: 10, height : 20 } => '{"height":10,"width":20}' */ function getJSON(obj) { - throw new Error('Not implemented'); + return JSON.stringify(obj); } @@ -55,6 +60,9 @@ function getJSON(obj) { */ function fromJSON(proto, json) { throw new Error('Not implemented'); + /*let obj = Object.create(proto); + obj = JSON.parse(json); + return obj;*/ } diff --git a/task/09-functions-n-closures-tasks.js b/task/09-functions-n-closures-tasks.js index 6ba9dcedd..aaafd9401 100644 --- a/task/09-functions-n-closures-tasks.js +++ b/task/09-functions-n-closures-tasks.js @@ -26,7 +26,9 @@ * */ function getComposition(f,g) { - throw new Error('Not implemented'); + return function (x) { + return f(g(x)); + } } @@ -47,7 +49,9 @@ function getComposition(f,g) { * */ function getPowerFunction(exponent) { - throw new Error('Not implemented'); + return function (number) { + return Math.pow(number, exponent); + }; } @@ -65,7 +69,10 @@ function getPowerFunction(exponent) { * getPolynom() => null */ function getPolynom() { - throw new Error('Not implemented'); + var tmp = Array.from(arguments).reverse(); + return (x) => { + return tmp.reduce((prev, curr, index) => prev+curr*Math.pow(x,index),0); + }; } @@ -84,7 +91,10 @@ function getPolynom() { * memoizer() => the same random number (next run, returns the previous cached result) */ function memoize(func) { - throw new Error('Not implemented'); + let cashed = func(); + return function() { + return cashed + } } @@ -104,7 +114,16 @@ function memoize(func) { * retryer() => 2 */ function retry(func, attempts) { - throw new Error('Not implemented'); + return()=>{ + for (var i = 0; i < attempts;){ + try{ + return func(); + } catch(err){ + i += 1; + } + } + return i; + }; } @@ -132,7 +151,16 @@ function retry(func, attempts) { * */ function logger(func, logFunc) { - throw new Error('Not implemented'); + return function(){ + let tmp = Array.from(arguments); + let callStr = JSON.stringify(tmp); + callStr = callStr.substr(1, callStr.length - 2); + callStr = `${func.name}(${callStr})`; + logFunc(callStr + " starts"); + let res = func.apply(null, tmp); + logFunc(callStr + " ends"); + return res; + } } @@ -150,7 +178,10 @@ function logger(func, logFunc) { * partialUsingArguments(fn, 'a','b','c','d')() => 'abcd' */ function partialUsingArguments(fn) { - throw new Error('Not implemented'); + var applyArgs = Array.prototype.slice.call(arguments, 1); + return function() { + return fn.apply(null, Array.prototype.concat(applyArgs, Array.prototype.slice.call(arguments))); + }; } @@ -171,7 +202,10 @@ function partialUsingArguments(fn) { * getId10() => 11 */ function getIdGeneratorFunction(startFrom) { - throw new Error('Not implemented'); + var curr = startFrom; + return function() { + return startFrom++; + } } diff --git a/task/10-katas-1-tasks.js b/task/10-katas-1-tasks.js index 5128b0695..74722ca2a 100644 --- a/task/10-katas-1-tasks.js +++ b/task/10-katas-1-tasks.js @@ -17,10 +17,58 @@ * ] */ function createCompassPoints() { - throw new Error('Not implemented'); - var sides = ['N','E','S','W']; // use array of cardinal directions only! -} + var sides = ['N', 'E', 'S', 'W'], // use array of cardinal directions only! + res = []; + + function getDoubleSide(side1, side2) { + switch (side1) { + case 'E': + return 'SE'; + case 'W': + return 'NW'; + default: + return side1 + side2; + } + } + for (var i = 0, currIndex, curr, next, newObj; i < 32; i++) { + newObj = {}; + currIndex = Math.trunc(i / 8); + curr = sides[currIndex]; + // prev = sides[curr > 0 ? curr - 1 : sides.length - 1]; + next = sides[currIndex < sides.length - 1 ? currIndex + 1 : 0]; + // console.log(curr,next); + switch (i % 8) { + case 0: + newObj['abbreviation'] = curr; + break; + case 1: + newObj['abbreviation'] = `${curr}b${next}`; + break; + case 2: + newObj['abbreviation'] = `${curr}${getDoubleSide(curr, next)}`; + break; + case 3: + newObj['abbreviation'] = `${getDoubleSide(curr, next)}b${curr}`; + break; + case 4: + newObj['abbreviation'] = getDoubleSide(curr, next); + break; + case 5: + newObj['abbreviation'] = `${getDoubleSide(curr, next)}b${next}`; + break; + case 6: + newObj['abbreviation'] = `${next}${getDoubleSide(curr, next)}`; + break; + case 7: + newObj['abbreviation'] = `${next}b${curr}`; + break; + } + newObj['azimuth'] = 11.25 * i; + res.push(newObj); + } + return res; +} /** * Expand the braces of the specified string. @@ -56,10 +104,54 @@ function createCompassPoints() { * 'nothing to do' => 'nothing to do' */ function* expandBraces(str) { - throw new Error('Not implemented'); + var search = '', + startIndex = -1, endIndex = -1, stack = [], tmpstr = []; + startIndex = str.indexOf('{'); + function outerSplit (string, symbol) { + var res = [], currStart = 0, stack = []; + for (var i = 0; i < string.length; i++) { + if (string.charAt(i) == '{') { + stack.push(0); + } else { + if (string.charAt(i) == '}') { + stack.pop(); + } else { + if (string.charAt(i) == symbol && stack.length == 0) { + res.push(string.slice(currStart, i)); + currStart = i + 1; + } + } + } + } + res.push(string.slice(currStart)) + return res; + } + if(startIndex != -1){ + for (endIndex = startIndex + 1; endIndex < str.length; endIndex++){ + if (str.charAt(endIndex) === '}') { + if (stack.length === 0) { + break; + } else { + stack.pop(); + } + } + if(str.charAt(endIndex) === '{') { + stack.push(0); + } + } + search = str.slice(startIndex, endIndex + 1); + } + if (search == '') { + yield str; + } else { + // tmpstr = search.slice(1,-1).split(','); + tmpstr = outerSplit(search.slice(1,-1), ','); + for (var i = 0; i < tmpstr.length; i++) { + yield * expandBraces(str.replace(search, tmpstr[i])); + } + } } - /** * Returns the ZigZag matrix * @@ -88,7 +180,26 @@ function* expandBraces(str) { * */ function getZigZagMatrix(n) { - throw new Error('Not implemented'); + let res = new Array(n).fill().map(()=> new Array(n).fill()); + let i = 0, j = 0; + let d = -1; + let start = 0, end = n*n - 1; + do { + res[i][j] = start++; + res[n - i - 1][n - j - 1] = end--; + i += d; + j -= d; + if (i < 0){ + i++; + d = -d; + } else if (j < 0){ + j++; + d = -d; + } + } while (start < end); + if (start === end) + res[i][j] = start; + return res; } @@ -113,7 +224,7 @@ function getZigZagMatrix(n) { * */ function canDominoesMakeRow(dominoes) { - throw new Error('Not implemented'); + return dominoes.map(x => x[0] + x[1]).reduce((prev, cur) => prev + cur) %2 != 0; } @@ -137,7 +248,16 @@ function canDominoesMakeRow(dominoes) { * [ 1, 2, 4, 5] => '1,2,4,5' */ function extractRanges(nums) { - throw new Error('Not implemented'); + for(var i = 0; i < nums.length; i++){ + var j = i; + while(nums[j] - nums[j + 1] == -1) { + j++; + } + if(j != i && j - i > 1) { + nums.splice(i, j - i + 1, nums[i] + '-' + nums[j]); + } + } + return nums.join(); } module.exports = { diff --git a/task/11-katas-2-tasks.js b/task/11-katas-2-tasks.js index 2a1a745cd..e31289281 100644 --- a/task/11-katas-2-tasks.js +++ b/task/11-katas-2-tasks.js @@ -34,7 +34,44 @@ * */ function parseBankAccount(bankAccount) { - throw new Error('Not implemented'); + let Arrtmp = bankAccount.split('\n'); + let accNum = 0; + let numIfDig = 9; + for (let i = 0; i < numIfDig; i++){ + let tmpd; + let k = i*3; + if (Arrtmp[2][k+2] === ' '){ + tmpd = 2; + } else if (Arrtmp[1][k+2] === ' '){ + if (Arrtmp[2][k] === ' '){ + tmpd = 5; + } else { + tmpd = 6; + } + } else if (Arrtmp[2][k] === '|'){ + if (Arrtmp[1][k+1] === ' '){ + tmpd = 0; + } else { + tmpd = 8; + } + } else if (Arrtmp[0][k+1] === ' '){ + if (Arrtmp[1][k+1] === ' '){ + tmpd = 1; + } else { + tmpd = 4; + } + } else if (Arrtmp[1][k+1] === '_'){ + if (Arrtmp[1][k] === '|'){ + tmpd = 9; + } else { + tmpd = 3; + } + } else{ + tmpd = 7; + } + accNum += tmpd * Math.pow(10, numIfDig - i - 1); + } + return accNum; } @@ -63,7 +100,18 @@ function parseBankAccount(bankAccount) { * 'characters.' */ function* wrapText(text, columns) { - throw new Error('Not implemented'); + var words = text.split(' '), curr = ''; + for (var i = 0; i < words.length; i++) { + if (curr.length + words[i].length <= columns) { + curr += words[i] + " "; + } else { + yield curr.trim(); + curr = words[i] + " "; + } + } + if (curr != '') { + yield curr.trim(); + } } @@ -100,20 +148,38 @@ const PokerRank = { } function getPokerHandRank(hand) { - throw new Error('Not implemented'); + const suites = '♥♠♦♣', + numbers = 'A234567891JQK'; + let suitArr = Array.from(suites, () => 0), + numArr = Array.from(numbers, () => 0); + for (let card of hand) { + suitArr[suites.indexOf(card.slice(-1))]++; + numArr[numbers.indexOf(card[0])]++; + } + numArr.push(numArr[0]); // Ace card + let suitStr = suitArr.join(''), + numStr = numArr.join(''); + return (numStr.indexOf('11111') !== -1) && + (suitStr.indexOf('5') !== -1) ? PokerRank.StraightFlush : + (numStr.indexOf('4') !== -1) ? PokerRank.FourOfKind : + (numStr.indexOf('2') !== -1) && (numStr.indexOf('3') !== -1) ? PokerRank.FullHouse : + (suitStr.indexOf('5') !== -1) ? PokerRank.Flush : + (numStr.indexOf('11111') !== -1) ? PokerRank.Straight : + (numStr.indexOf('3') !== -1) ? PokerRank.ThreeOfKind : + (numStr.match(/2.*2.+/)) ? PokerRank.TwoPairs : + (numStr.indexOf('2') !== -1) ? PokerRank.OnePair : + PokerRank.HighCard; } - - /** * Returns the rectangles sequence of specified figure. * The figure is ASCII multiline string comprised of minus signs -, plus signs +, vertical bars | and whitespaces. * The task is to break the figure in the rectangles it is made of. * * NOTE: The order of rectanles does not matter. - * + * * @param {string} figure * @return {Iterable.} decomposition to basic parts - * + * * @example * * '+------------+\n'+ @@ -135,7 +201,96 @@ function getPokerHandRank(hand) { * '+-------------+\n' */ function* getFigureRectangles(figure) { - throw new Error('Not implemented'); + const Arrtmp = figure.split('\n'); + const pluses = []; + const horizontalLines = []; + const rectangles = []; + + for (let i = 0; i < Arrtmp.length; i++) + for (let j = 0; j < Arrtmp[0].length; j++) + if (Arrtmp[i][j] === '+') { + pluses.push({x: j, y: i}); + } + + for (let i = 0; i < pluses.length; i++) + for (let j = i + 1; j < pluses.length; j++) + if (pluses[i].y === pluses[j].y) { + if (checkHorizontalLine(Arrtmp, pluses[i], pluses[j])) + horizontalLines.push([pluses[i], pluses[j]]); + } + + for (let i = 0; i < horizontalLines.length; i++) + for (let j = i + 1; j < horizontalLines.length; j++) + if (checkRectangle(Arrtmp, horizontalLines[i], horizontalLines[j])) { + rectangles.push([horizontalLines[i], horizontalLines[j]]); + } + + for (let i = 0; i < rectangles.length; i++) { + let rectangle = drawRectangle(rectangles[i]); + + yield rectangle; + } +} + +function checkHorizontalLine(Arrtmp, s, f) { + for (let i = s.x; i <= f.y; i++) + if (Arrtmp[s.y][i] !== '-' && Arrtmp[s.y][i] !== '+') + return false; + + return true; +} + +function checkRectangle(Arrtmp, top, bottom) { + if (top[0].x !== bottom[0].x) + return false; + + if (top[1].x !== bottom[1].x) + return false; + + const leftX = top[0].x, + rightX = top[1].x, + topY = top[0].y, + bottomY = bottom[0].y; + + for (let j = leftX + 1; j < rightX; j++) + if (Arrtmp[topY][j] === '+' && Arrtmp[bottomY][j] === '+') { + let hasWhiteSpace = false; + + for (let i = topY + 1; i < bottomY; i++) + if (Arrtmp[i][j] === ' ') + hasWhiteSpace = true; + + + if (!hasWhiteSpace) + return false; + } + + for (let i = topY + 1; i < bottomY; i++) { + if (Arrtmp[i][leftX] !== '|' && Arrtmp[i][leftX] !== '+') + return false; + + if (Arrtmp[i][rightX] !== '|' && Arrtmp[i][rightX] !== '+') + return false; + + for (let j = leftX + 1; j < rightX; j++) + if (Arrtmp[i][j] !== ' ') + return false; + } + + return true; +} + +function drawRectangle(item) { + let width = item[0][1].x - item[0][0].x + 1, + height = item[1][0].y - item[0][0].y + 1, + result = '', + topLine = '+' + ('-').repeat(width - 2) + '+' + '\n'; + + result += topLine; + result += ( '|' + (' ').repeat(width - 2) + '|' + '\n' ).repeat(height - 2); + result += topLine; + + return result; } diff --git a/task/12-katas-3-tasks.js b/task/12-katas-3-tasks.js index 5299be87d..ae4cd33af 100644 --- a/task/12-katas-3-tasks.js +++ b/task/12-katas-3-tasks.js @@ -10,13 +10,13 @@ * @return {bool} * * @example - * var puzzle = [ + * var puzzle = [ * 'ANGULAR', * 'REDNCAE', * 'RFIDTCL', * 'AGNEGSA', * 'YTIRTSP', - * ]; + * ]; * 'ANGULAR' => true (first row) * 'REACT' => true (starting from the top-right R adn follow the ↓ ← ← ↓ ) * 'UNDEFINED' => true @@ -25,10 +25,42 @@ * 'CLASS' => true * 'ARRAY' => true (first column) * 'FUNCTION' => false - * 'NULL' => false + * 'NULL' => false */ function findStringInSnakingPuzzle(puzzle, searchStr) { - throw new Error('Not implemented'); + function dfs(current, step) { + let save = data[current.y][current.x]; + data[current.y][current.x] = ""; + if (step == search.length) + return true; + let result = false, + steps = [[1, 0], [-1, 0], [0, -1], [0, 1]]; + for (let i = 0; i < 4; i++) { + let newX = current.x + steps[i][0], + newY = current.y + steps[i][1]; + if (data[newY][newX] == search[step]) { + result = result || dfs({x: newX, y: newY}, step + 1); + } + } + data[current.y][current.x] = save; + return result; + } + let data = Array.from(puzzle); + let Arrtmp = Array.from({length: data[0].length + 2}, () => ''); + data = data.map(item => [''].concat(item.split(''), [''])); + data = [Arrtmp].concat(data, [Arrtmp]); + let search = searchStr.split(''), + n = data[0].length - 1, + m = data.length - 1; + for (let i = 1; i < m; i++) { + for (let j = 1; j < n; j++) + if (data[i][j] == search[0]) { + if (dfs({y: i, x: j}, 1)) + return true; + } + } + + return false; } @@ -36,7 +68,7 @@ function findStringInSnakingPuzzle(puzzle, searchStr) { * Returns all permutations of the specified string. * Assume all chars in the specified string are different. * The order of permutations does not matter. - * + * * @param {string} chars * @return {Iterable.} all posible strings constructed with the chars from the specfied string * @@ -45,7 +77,20 @@ function findStringInSnakingPuzzle(puzzle, searchStr) { * 'abc' => 'abc','acb','bac','bca','cab','cba' */ function* getPermutations(chars) { - throw new Error('Not implemented'); + function *permute(a, len){ + if (len < 2) + yield a.join(''); + else{ + for (let i = 0; i < len; i++){ + yield *permute(a,len - 1); + const toSwap = len % 2 ? 0 : i; + let tmp = a[len - 1]; + a[len - 1] = a[toSwap]; + a[toSwap] = tmp; + } + } + } + yield *permute(chars.split(''), chars.length); } @@ -53,9 +98,9 @@ function* getPermutations(chars) { * Returns the most profit from stock quotes. * Stock quotes are stores in an array in order of date. * The stock profit is the difference in prices in buying and selling stock. - * Each day, you can either buy one unit of stock, sell any number of stock units you have already bought, or do nothing. + * Each day, you can either buy one unit of stock, sell any number of stock units you have already bought, or do nothing. * Therefore, the most profit is the maximum difference of all pairs in a sequence of stock prices. - * + * * @param {array} quotes * @return {number} max profit * @@ -65,7 +110,18 @@ function* getPermutations(chars) { * [ 1, 6, 5, 10, 8, 7 ] => 18 (buy at 1,6,5 and sell all at 10) */ function getMostProfitFromStockQuotes(quotes) { - throw new Error('Not implemented'); + let tmpMax = quotes + .reduceRight((prev, curr)=>{ + if(prev.length == 0) + return [curr]; + prev.push(Math.max(prev[prev.length - 1],curr)); + return prev; + }, []) + .reverse(); + tmpMax.push(0); + return quotes.reduce((prev, curr, index)=>{ + return prev + Math.max(0, tmpMax[index + 1] - curr); + }, 0); } @@ -73,31 +129,49 @@ function getMostProfitFromStockQuotes(quotes) { * Class representing the url shorting helper. * Feel free to implement any algorithm, but do not store link in the key\value stores. * The short link can be at least 1.5 times shorter than the original url. - * + * * @class * * @example - * + * * var urlShortener = new UrlShortener(); * var shortLink = urlShortener.encode('https://en.wikipedia.org/wiki/URL_shortening'); * var original = urlShortener.decode(shortLink); // => 'https://en.wikipedia.org/wiki/URL_shortening' - * + * */ function UrlShortener() { this.urlAllowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"+ - "abcdefghijklmnopqrstuvwxyz"+ - "0123456789-_.~!*'();:@&=+$,/?#[]"; + "abcdefghijklmnopqrstuvwxyz"+ + "0123456789-_.~!*'();:@&=+$,/?#[]"; } UrlShortener.prototype = { encode: function(url) { - throw new Error('Not implemented'); + let res = ""; + for (let i = 0; i < url.length; i += 2) { + let tmp1 = url.charCodeAt(i); + let tmp2 = url.charCodeAt(i + 1); + let code = (tmp1 << 8) |tmp2; + res += String.fromCharCode(code); + } + return res; }, - + decode: function(code) { - throw new Error('Not implemented'); - } + let res = ""; + for (let i = 0; i < code.length; i++) { + let char = parseInt(code.charCodeAt(i), 10); + let tmp1 = char & 255; + let tmp2 = (char >> 8) & 255; + if (tmp1 === 0) { + res += String.fromCharCode(tmp2) + } else { + res += String.fromCharCode(tmp2) + String.fromCharCode(tmp1); + } + } + return res; + } } diff --git a/test/01-strings-tests.js b/test/01-strings-tests.js index e044644e5..cfb8cf3cc 100644 --- a/test/01-strings-tests.js +++ b/test/01-strings-tests.js @@ -73,21 +73,21 @@ describe('01-strings-tasks', function() { it.optional('getRectangleString should return the string reprentation of rectangle with specified size', function() { assert.equal( - '┌────┐\n'+ - '│ │\n'+ - '│ │\n'+ - '└────┘\n', + '┌────┐\n'+ + '│ │\n'+ + '│ │\n'+ + '└────┘\n', tasks.getRectangleString(6, 4) ); assert.deepEqual( - '┌┐\n'+ - '└┘\n', + '┌┐\n'+ + '└┘\n', tasks.getRectangleString(2, 2) ); assert.deepEqual( - '┌──────────┐\n'+ - '│ │\n'+ - '└──────────┘\n', + '┌──────────┐\n'+ + '│ │\n'+ + '└──────────┘\n', tasks.getRectangleString(12, 3) ); }); @@ -109,13 +109,13 @@ describe('01-strings-tasks', function() { assert.equal(true, tasks.isString('test'), "test"); assert.equal(true, tasks.isString(new String('test')), "new String('test')"); }); - + it.optional('getCardId should return the index of card in the initial deck', function() { [ - '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♠' + '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♠' ].forEach((val, index) => { assert.equal( index, @@ -123,6 +123,6 @@ describe('01-strings-tasks', function() { `Invalid id for card '${val}':` ) }); - + }); }); diff --git a/test/02-numbers-tests.js b/test/02-numbers-tests.js index d86b3e940..f58bad8bd 100644 --- a/test/02-numbers-tests.js +++ b/test/02-numbers-tests.js @@ -36,7 +36,7 @@ describe('02-numbers-tasks', function() { assert.equal(-8, tasks.getLinearEquationRoot(1, 8)); assert.equal(0, tasks.getLinearEquationRoot(5, 0)); }); - + it.optional('getAngleBetweenVectors should return a angle (in radians) between two linear vectors', function() { assert.equal(Math.PI/2, tasks.getAngleBetweenVectors(1, 0, 0, 1)); assert.equal(Math.PI, tasks.getAngleBetweenVectors(0, 1, 0, -1)); @@ -68,7 +68,7 @@ describe('02-numbers-tasks', function() { assert.equal(1230, tasks.roundToPowerOfTen(1234,1)); assert.equal(1200, tasks.roundToPowerOfTen(1234,2)); assert.equal(1000, tasks.roundToPowerOfTen(1234,3)); - + assert.equal(9678, tasks.roundToPowerOfTen(9678,0)); assert.equal(9680, tasks.roundToPowerOfTen(9678,1)); assert.equal(9700, tasks.roundToPowerOfTen(9678,2)); diff --git a/test/03-date-tests.js b/test/03-date-tests.js index 8423865b0..995809b61 100644 --- a/test/03-date-tests.js +++ b/test/03-date-tests.js @@ -68,22 +68,22 @@ describe('03-date-tasks', function() { endDate: new Date(2000,1,1,11,0,0), expected: '01:00:00.000' }, { - startDate: new Date(2000,1,1,10,0,0), - endDate: new Date(2000,1,1,10,30,0), - expected: '00:30:00.000' - }, { - startDate: new Date(2000,1,1,10,0,0), - endDate: new Date(2000,1,1,10,0,20), - expected: '00:00:20.000' - }, { - startDate: new Date(2000,1,1,10,0,0), - endDate: new Date(2000,1,1,10,0,0,250), - expected: '00:00:00.250' - }, { - 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,1,10,30,0), + expected: '00:30:00.000' + }, { + startDate: new Date(2000,1,1,10,0,0), + endDate: new Date(2000,1,1,10,0,20), + expected: '00:00:20.000' + }, { + startDate: new Date(2000,1,1,10,0,0), + endDate: new Date(2000,1,1,10,0,0,250), + expected: '00:00:00.250' + }, { + startDate: new Date(2000,1,1,10,0,0), + endDate: new Date(2000,1,1,15,20,10,453), + expected: '05:20:10.453' + } ].forEach(data => { assert.equal( data.expected, @@ -92,43 +92,43 @@ describe('03-date-tasks', function() { }); }); - - + + it.optional('angleBetweenClockHands should returns the angle bettween clock hands for specified Greenwich datetime', function () { [ { date: Date.UTC(2016,3,5, 0, 0), expected: 0 }, { - date: Date.UTC(2016,3,5, 3, 0), - expected: Math.PI/2 - }, { - date: Date.UTC(2016,3,5,15, 0), - expected: Math.PI/2 - }, { - date: Date.UTC(2016,3,5, 6, 0), - expected: Math.PI - }, { - date: Date.UTC(2016,3,5,18, 0), - expected: Math.PI - }, { - date: Date.UTC(2016,3,5, 9, 0), - expected: Math.PI/2 - }, { - date: Date.UTC(2016,3,5,21, 0), - expected: Math.PI/2 - }, { - date: Date.UTC(2016,3,5,14,20), - expected: 0.8726646259971648 - }, { - date: Date.UTC(2016,3,5,23,55), - expected: 0.4799655442984406 - } + date: Date.UTC(2016,3,5, 3, 0), + expected: Math.PI/2 + }, { + date: Date.UTC(2016,3,5,15, 0), + expected: Math.PI/2 + }, { + date: Date.UTC(2016,3,5, 6, 0), + expected: Math.PI + }, { + date: Date.UTC(2016,3,5,18, 0), + expected: Math.PI + }, { + date: Date.UTC(2016,3,5, 9, 0), + expected: Math.PI/2 + }, { + date: Date.UTC(2016,3,5,21, 0), + expected: Math.PI/2 + }, { + date: Date.UTC(2016,3,5,14,20), + expected: 0.8726646259971648 + }, { + date: Date.UTC(2016,3,5,23,55), + expected: 0.4799655442984406 + } ].forEach(data => { assert.equal( tasks.angleBetweenClockHands(new Date(data.date)), data.expected, - `Incorrect result for angleBetweenClockHands(${new Date(data.date).toUTCString()}):` + `Incorrect result for angleBetweenClockHands(${new Date(data.date).toUTCString()}):` ); }); }); diff --git a/test/04-arrays-tests.js b/test/04-arrays-tests.js index eef6f904f..d29e71854 100644 --- a/test/04-arrays-tests.js +++ b/test/04-arrays-tests.js @@ -13,14 +13,14 @@ describe('04-arrays-tasks', function() { value: 10, expected: 1 },{ - arr: ['Array', 'Number', 'string'], - value: 'Date', - expected: -1 - },{ - arr: [0, 1, 2, 3, 4, 5], - value: 5, - expected: 5 - } + arr: ['Array', 'Number', 'string'], + value: 'Date', + expected: -1 + },{ + arr: [0, 1, 2, 3, 4, 5], + value: 5, + expected: 5 + } ].forEach(data => { var actual = tasks.findElement(data.arr, data.value); assert.equal( @@ -38,15 +38,15 @@ describe('04-arrays-tasks', function() { len: 1, expected: [ 1 ] },{ - len: 2, - expected: [ 1, 3 ] - },{ - len: 5, - expected: [ 1, 3, 5, 7, 9 ] - },{ - len: 16, - expected: [ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 ] - } + len: 2, + expected: [ 1, 3 ] + },{ + len: 5, + expected: [ 1, 3, 5, 7, 9 ] + },{ + len: 16, + expected: [ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 ] + } ].forEach(data => { assert.deepEqual( data.expected, @@ -62,12 +62,12 @@ describe('04-arrays-tasks', function() { arr: ['Ace', 10, true], expected: ['Ace', 10, true, 'Ace', 10, true] },{ - arr: [0, 1, 2, 3, 4, 5], - expected: [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5] - },{ - arr: [], - expected: [] - } + arr: [0, 1, 2, 3, 4, 5], + expected: [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5] + },{ + arr: [], + expected: [] + } ].forEach(data => { var actual = tasks.doubleArray(data.arr); assert.deepEqual( @@ -85,12 +85,12 @@ describe('04-arrays-tasks', function() { arr: [ 0, 1, 2, 3, 4, 5 ], expected: [ 1, 2, 3, 4, 5 ] },{ - arr: [-1, 2, -5, -4, 0], - expected: [ 2 ] - },{ - arr: [], - expected: [] - } + arr: [-1, 2, -5, -4, 0], + expected: [ 2 ] + },{ + arr: [], + expected: [] + } ].forEach(data => { var actual = tasks.getArrayOfPositives(data.arr); assert.deepEqual( @@ -107,12 +107,12 @@ describe('04-arrays-tasks', function() { arr: [ 0, 1, 'cat', 3, true, 'dog' ], expected: [ 'cat', 'dog' ] },{ - arr: [ 1, 2, 3, 4, 5 ], - expected: [ ] - },{ - arr: [ 'cat', 'dog', 'raccon' ], - expected: [ 'cat', 'dog', 'raccon' ] - } + arr: [ 1, 2, 3, 4, 5 ], + expected: [ ] + },{ + arr: [ 'cat', 'dog', 'raccon' ], + expected: [ 'cat', 'dog', 'raccon' ] + } ].forEach(data => { var actual = tasks.getArrayOfStrings(data.arr); assert.deepEqual( @@ -129,12 +129,12 @@ describe('04-arrays-tasks', function() { arr: [ 0, false, 'cat', NaN, true, '' ], expected: [ 'cat', true ] },{ - arr: [ 1, 2, 3, 4, 5, 'false' ], - expected: [ 1, 2, 3, 4, 5, 'false' ] - },{ - arr: [ false, 0, NaN, '', undefined ], - expected: [ ] - } + arr: [ 1, 2, 3, 4, 5, 'false' ], + expected: [ 1, 2, 3, 4, 5, 'false' ] + },{ + arr: [ false, 0, NaN, '', undefined ], + expected: [ ] + } ].forEach(data => { var actual = tasks.removeFalsyValues(data.arr); assert.deepEqual( @@ -152,22 +152,22 @@ describe('04-arrays-tasks', function() { item: 1, expected: 3 },{ - arr: [ 1, 2, 3, 4, 5 ], - item: 0, - expected: 0 - },{ - arr: [ 'a','b','c','c' ], - item: 'c', - expected: 2 - },{ - arr: [ null, undefined, null ], - item: null, - expected: 2 - },{ - arr: [ true, 0, 1, 'true' ], - item: true, - expected: 1 - } + arr: [ 1, 2, 3, 4, 5 ], + item: 0, + expected: 0 + },{ + arr: [ 'a','b','c','c' ], + item: 'c', + expected: 2 + },{ + arr: [ null, undefined, null ], + item: null, + expected: 2 + },{ + arr: [ true, 0, 1, 'true' ], + item: true, + expected: 1 + } ].forEach(data => { var actual = tasks.findAllOccurences(data.arr, data.item); assert.equal( @@ -185,9 +185,9 @@ describe('04-arrays-tasks', function() { arr: [ 'permanent-internship', 'glutinous-shriek', 'multiplicative-elevation' ], expected: [ 'PERMANENT-INTERNSHIP', 'GLUTINOUS-SHRIEK', 'MULTIPLICATIVE-ELEVATION' ] },{ - arr: [ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ], - expected: [ 'A', 'B', 'C', 'D', 'E', 'F', 'G' ] - } + arr: [ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ], + expected: [ 'A', 'B', 'C', 'D', 'E', 'F', 'G' ] + } ].forEach(data => { var actual = tasks.getUpperCaseStrings(data.arr); assert.deepEqual( @@ -204,9 +204,9 @@ describe('04-arrays-tasks', function() { arr: [ '', 'a', 'bc', 'def', 'ghij' ], expected: [ 0, 1, 2, 3, 4 ] },{ - arr: [ 'angular', 'react', 'ember' ], - expected: [ 7, 5, 5 ] - } + arr: [ 'angular', 'react', 'ember' ], + expected: [ 7, 5, 5 ] + } ].forEach(data => { var actual = tasks.getStringsLength(data.arr); assert.deepEqual( @@ -225,11 +225,11 @@ describe('04-arrays-tasks', function() { index: 1, expected: [ 1, 2, 3, 4, 5 ] },{ - arr: [ 1, 'b', 'c' ], - item: 'x', - index: 0, - expected: [ 'x', 1, 'b', 'c' ] - } + arr: [ 1, 'b', 'c' ], + item: 'x', + index: 0, + expected: [ 'x', 1, 'b', 'c' ] + } ].forEach(data => { tasks.insertItem(data.arr, data.item, data.index); assert.deepEqual( @@ -247,10 +247,10 @@ describe('04-arrays-tasks', function() { n: 2, expected: [ 1, 2 ] },{ - arr: [ 'a', 'b', 'c', 'd' ], - n: 3, - expected: [ 'a', 'b', 'c' ] - } + arr: [ 'a', 'b', 'c', 'd' ], + n: 3, + expected: [ 'a', 'b', 'c' ] + } ].forEach(data => { assert.deepEqual( data.expected, @@ -267,10 +267,10 @@ describe('04-arrays-tasks', function() { n: 2, expected: [ 4, 5 ] },{ - arr: [ 'a', 'b', 'c', 'd' ], - n: 3, - expected: [ 'b', 'c', 'd' ] - } + arr: [ 'a', 'b', 'c', 'd' ], + n: 3, + expected: [ 'b', 'c', 'd' ] + } ].forEach(data => { assert.deepEqual( data.expected, @@ -284,20 +284,20 @@ describe('04-arrays-tasks', function() { [ { arr: [ - [ 0, 1, 2, 3, 4 ], - [ 10,11,12,13,14 ], - [ 20,21,22,23,24 ], - [ 30,31,32,33,34 ] + [ 0, 1, 2, 3, 4 ], + [ 10,11,12,13,14 ], + [ 20,21,22,23,24 ], + [ 30,31,32,33,34 ] ], expected: - '0,1,2,3,4\n' - +'10,11,12,13,14\n' - +'20,21,22,23,24\n' - +'30,31,32,33,34' - }, { - arr: [[]], - expected: '' - } + '0,1,2,3,4\n' + +'10,11,12,13,14\n' + +'20,21,22,23,24\n' + +'30,31,32,33,34' + }, { + arr: [[]], + expected: '' + } ].forEach(data => { var actual = tasks.toCsvText(data.arr); assert.equal( @@ -314,9 +314,9 @@ describe('04-arrays-tasks', function() { arr: [ 0, 1, 2, 3, 4, 5 ], expected: [ 0, 1, 4, 9, 16, 25 ] }, { - arr: [ 10, 100, -1 ], - expected: [ 100, 10000, 1 ] - } + arr: [ 10, 100, -1 ], + expected: [ 100, 10000, 1 ] + } ].forEach(data => { var actual = tasks.toArrayOfSquares(data.arr); assert.deepEqual( @@ -333,15 +333,15 @@ describe('04-arrays-tasks', function() { arr: [ 1, 1, 1, 1, 1 ], expected: [ 1, 2, 3, 4, 5 ] }, { - arr: [ 10, -10, 10, -10, 10 ], - expected: [ 10, 0, 10, 0, 10 ] - }, { - arr: [ 0, 0, 0, 0, 0], - expected: [ 0, 0, 0, 0, 0] - }, { - arr: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], - expected: [ 1, 3, 6, 10, 15, 21, 28, 36, 45, 55 ] - } + arr: [ 10, -10, 10, -10, 10 ], + expected: [ 10, 0, 10, 0, 10 ] + }, { + arr: [ 0, 0, 0, 0, 0], + expected: [ 0, 0, 0, 0, 0] + }, { + arr: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], + expected: [ 1, 3, 6, 10, 15, 21, 28, 36, 45, 55 ] + } ].forEach(data => { var actual = tasks.getMovingSum(data.arr); assert.deepEqual( @@ -358,12 +358,12 @@ describe('04-arrays-tasks', function() { arr: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], expected: [ 2, 4, 6, 8, 10 ] }, { - arr: [ 'a', 'b', 'c' , null ], - expected: [ "b", null ] - }, { - arr: [ "a" ], - expected: [ ] - } + arr: [ 'a', 'b', 'c' , null ], + expected: [ "b", null ] + }, { + arr: [ "a" ], + expected: [ ] + } ].forEach(data => { var actual = tasks.getSecondItems(data.arr); assert.deepEqual( @@ -379,19 +379,19 @@ describe('04-arrays-tasks', function() { { arr: [], expected: [] - }, { - arr: [ 1 ], - expected: [ 1 ] - }, { - arr: [ 'a', 'b' ], - expected: [ 'a', 'b','b' ] - }, { - arr: [ 'a', 'b', 'c', null ], - expected: [ 'a', 'b','b', 'c','c','c', null,null,null,null ] }, { - arr: [ 1, 2, 3, 4, 5 ], - expected: [ 1, 2,2, 3,3,3, 4,4,4,4, 5,5,5,5,5 ] - } + arr: [ 1 ], + expected: [ 1 ] + }, { + arr: [ 'a', 'b' ], + expected: [ 'a', 'b','b' ] + }, { + arr: [ 'a', 'b', 'c', null ], + expected: [ 'a', 'b','b', 'c','c','c', null,null,null,null ] + }, { + arr: [ 1, 2, 3, 4, 5 ], + expected: [ 1, 2,2, 3,3,3, 4,4,4,4, 5,5,5,5,5 ] + } ].forEach(data => { var actual = tasks.propagateItemsByPositionIndex(data.arr); assert.deepEqual( @@ -407,19 +407,19 @@ describe('04-arrays-tasks', function() { { arr: [], expected: [] - }, { - arr: [ 1,2 ], - expected: [ 2,1 ] }, { - arr: [ 1, 2, 3 ], - expected: [ 3, 2, 1 ] - }, { - arr: [ 1,2,3,4,5,6,7,8,9,10 ], - expected: [ 10,9,8 ] - }, { - arr: [ 10, 10, 10, 10], - expected: [ 10, 10, 10 ] - } + arr: [ 1,2 ], + expected: [ 2,1 ] + }, { + arr: [ 1, 2, 3 ], + expected: [ 3, 2, 1 ] + }, { + arr: [ 1,2,3,4,5,6,7,8,9,10 ], + expected: [ 10,9,8 ] + }, { + arr: [ 10, 10, 10, 10], + expected: [ 10, 10, 10 ] + } ].forEach(data => { var actual = tasks.get3TopItems(data.arr); assert.deepEqual( @@ -435,19 +435,19 @@ describe('04-arrays-tasks', function() { { arr: [], expected: 0 - }, { - arr: [ -1, 0, 1 ], - expected: 1 - }, { - arr: [ 1, 2, 3 ], - expected: 3 }, { - arr: [ null, 1, 'elephant' ], - expected: 1 - }, { - arr: [ 1, '2' ], - expected: 1 - } + arr: [ -1, 0, 1 ], + expected: 1 + }, { + arr: [ 1, 2, 3 ], + expected: 3 + }, { + arr: [ null, 1, 'elephant' ], + expected: 1 + }, { + arr: [ 1, '2' ], + expected: 1 + } ].forEach(data => { var actual = tasks.getPositivesCount(data.arr); assert.equal( @@ -465,21 +465,21 @@ describe('04-arrays-tasks', function() { arr: [], expected: [] }, { - arr: [ 'nine','one' ], - expected: [ 'one', 'nine' ] - }, { - arr: [ 'one','two','three' ], - expected: [ 'one','two', 'three' ] - }, { - arr: [ 'nine','eight','nine','eight' ], - expected: [ 'eight','eight','nine','nine' ] - }, { - arr: [ 'one','one','one','zero' ], - expected: [ 'zero','one','one','one' ] - }, { - arr: [ 'nine','eight','seven','six','five','four','three','two','one','zero' ], - expected: [ 'zero','one','two','three','four','five','six','seven','eight','nine' ] - } + arr: [ 'nine','one' ], + expected: [ 'one', 'nine' ] + }, { + arr: [ 'one','two','three' ], + expected: [ 'one','two', 'three' ] + }, { + arr: [ 'nine','eight','nine','eight' ], + expected: [ 'eight','eight','nine','nine' ] + }, { + arr: [ 'one','one','one','zero' ], + expected: [ 'zero','one','one','one' ] + }, { + arr: [ 'nine','eight','seven','six','five','four','three','two','one','zero' ], + expected: [ 'zero','one','two','three','four','five','six','seven','eight','nine' ] + } ].forEach(data => { var actual = tasks.sortDigitNamesByNumericOrder(data.arr); assert.deepEqual( @@ -496,12 +496,12 @@ describe('04-arrays-tasks', function() { arr: [ ], expected: 0 },{ - arr: [ 1, 2, 3 ], - expected: 6 - },{ - arr: [ 1, 10, 100, 1000 ], - expected: 1111 - } + arr: [ 1, 2, 3 ], + expected: 6 + },{ + arr: [ 1, 10, 100, 1000 ], + expected: 1111 + } ].forEach(data => { var actual = tasks.getItemsSum(data.arr); assert.deepEqual( @@ -519,15 +519,15 @@ describe('04-arrays-tasks', function() { arr: [ ], expected: 0 },{ - arr: [ 1, '', 3 ], - expected: 1 - },{ - arr: [ -1, 'false', null, 0 ], - expected: 2 - },{ - arr: [ null, undefined, NaN, false, 0, '' ], - expected: 6 - } + arr: [ 1, '', 3 ], + expected: 1 + },{ + arr: [ -1, 'false', null, 0 ], + expected: 2 + },{ + arr: [ null, undefined, NaN, false, 0, '' ], + expected: 6 + } ].forEach(data => { var actual = tasks.getFalsyValuesCount(data.arr); assert.deepEqual( @@ -545,12 +545,12 @@ describe('04-arrays-tasks', function() { arr: [ 0, false, 'cat', NaN, true, '' ], expected: '0,false,cat,NaN,true,' },{ - arr: [ 1, 2, 3, 4, 5 ], - expected: '1,2,3,4,5' - },{ - arr: [ 'rock', 'paper', 'scissors' ], - expected: 'rock,paper,scissors' - } + arr: [ 1, 2, 3, 4, 5 ], + expected: '1,2,3,4,5' + },{ + arr: [ 'rock', 'paper', 'scissors' ], + expected: 'rock,paper,scissors' + } ].forEach(data => { var actual = tasks.toStringList(data.arr); assert.equal( @@ -565,68 +565,68 @@ describe('04-arrays-tasks', function() { [ { arr: [ - { country: 'Russia', city: 'Moscow' }, - { country: 'Belarus', city: 'Minsk' }, - { country: 'Poland', city: 'Warsaw' }, - { country: 'Russia', city: 'Saint Petersburg' }, - { country: 'Poland', city: 'Krakow' }, - { country: 'Belarus', city: 'Brest' } + { country: 'Russia', city: 'Moscow' }, + { country: 'Belarus', city: 'Minsk' }, + { country: 'Poland', city: 'Warsaw' }, + { country: 'Russia', city: 'Saint Petersburg' }, + { country: 'Poland', city: 'Krakow' }, + { country: 'Belarus', city: 'Brest' } ], expected: [ - { country: 'Belarus', city: 'Brest' }, - { country: 'Belarus', city: 'Minsk' }, - { country: 'Poland', city: 'Krakow' }, - { country: 'Poland', city: 'Warsaw' }, - { country: 'Russia', city: 'Moscow' }, - { country: 'Russia', city: 'Saint Petersburg' } + { country: 'Belarus', city: 'Brest' }, + { country: 'Belarus', city: 'Minsk' }, + { country: 'Poland', city: 'Krakow' }, + { country: 'Poland', city: 'Warsaw' }, + { country: 'Russia', city: 'Moscow' }, + { country: 'Russia', city: 'Saint Petersburg' } ] }, { - arr: [ - { country: 'D', city: '1' }, - { country: 'E', city: '1' }, - { country: 'A', city: '2' }, - { country: 'B', city: '1' }, - { country: 'B', city: '2' }, - { country: 'A', city: '1' } - ], - expected: [ - { country: 'A', city: '1' }, - { country: 'A', city: '2' }, - { country: 'B', city: '1' }, - { country: 'B', city: '2' }, - { country: 'D', city: '1' }, - { country: 'E', city: '1' } - ] - },{ - arr: [ - { country: '5', city: '1' }, - { country: '1', city: '1' }, - { country: '1', city: '2' }, - { country: '1', city: '3' }, - { country: '2', city: '2' }, - { country: '1', city: '1' }, - { country: '1', city: '1' }, - { country: '2', city: '1' }, - { country: '3', city: '1' }, - { country: '3', city: '3' }, - { country: '2', city: '5' }, - { country: '5', city: '2' } - ], - expected: [ - { country: '1', city: '1' }, - { country: '1', city: '1' }, - { country: '1', city: '1' }, - { country: '1', city: '2' }, - { country: '1', city: '3' }, - { country: '2', city: '1' }, - { country: '2', city: '2' }, - { country: '2', city: '5' }, - { country: '3', city: '1' }, - { country: '3', city: '3' }, - { country: '5', city: '1' }, - { country: '5', city: '2' } - ] - } + arr: [ + { country: 'D', city: '1' }, + { country: 'E', city: '1' }, + { country: 'A', city: '2' }, + { country: 'B', city: '1' }, + { country: 'B', city: '2' }, + { country: 'A', city: '1' } + ], + expected: [ + { country: 'A', city: '1' }, + { country: 'A', city: '2' }, + { country: 'B', city: '1' }, + { country: 'B', city: '2' }, + { country: 'D', city: '1' }, + { country: 'E', city: '1' } + ] + },{ + arr: [ + { country: '5', city: '1' }, + { country: '1', city: '1' }, + { country: '1', city: '2' }, + { country: '1', city: '3' }, + { country: '2', city: '2' }, + { country: '1', city: '1' }, + { country: '1', city: '1' }, + { country: '2', city: '1' }, + { country: '3', city: '1' }, + { country: '3', city: '3' }, + { country: '2', city: '5' }, + { country: '5', city: '2' } + ], + expected: [ + { country: '1', city: '1' }, + { country: '1', city: '1' }, + { country: '1', city: '1' }, + { country: '1', city: '2' }, + { country: '1', city: '3' }, + { country: '2', city: '1' }, + { country: '2', city: '2' }, + { country: '2', city: '5' }, + { country: '3', city: '1' }, + { country: '3', city: '3' }, + { country: '5', city: '1' }, + { country: '5', city: '2' } + ] + } ].forEach(data => { var actual = tasks.sortCitiesArray(data.arr); assert.deepEqual( @@ -643,17 +643,17 @@ describe('04-arrays-tasks', function() { n: 1, expected: [[1]] }, { - n: 2, - expected: [[1,0], - [0,1]] - }, { - n: 5, - expected: [[1,0,0,0,0], - [0,1,0,0,0], - [0,0,1,0,0], - [0,0,0,1,0], - [0,0,0,0,1]] - } + n: 2, + expected: [[1,0], + [0,1]] + }, { + n: 5, + expected: [[1,0,0,0,0], + [0,1,0,0,0], + [0,0,1,0,0], + [0,0,0,1,0], + [0,0,0,0,1]] + } ].forEach(data => { var actual = tasks.getIdentityMatrix(data.n); assert.deepEqual( @@ -671,25 +671,25 @@ describe('04-arrays-tasks', function() { end: 5, expected: [ 1, 2, 3, 4, 5 ] }, { - start: -2, - end: 2, - expected: [ -2, -1, 0, 1, 2 ] - }, { - start: 0, - end: 100, - expected: [ - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, - 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, - 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, - 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, - 100 - ] - }, { - start: 3, - end: 3, - expected: [ 3 ] - } + start: -2, + end: 2, + expected: [ -2, -1, 0, 1, 2 ] + }, { + start: 0, + end: 100, + expected: [ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, + 100 + ] + }, { + start: 3, + end: 3, + expected: [ 3 ] + } ].forEach(data => { var actual = tasks.getIntervalArray(data.start, data.end); assert.deepEqual( @@ -706,15 +706,15 @@ describe('04-arrays-tasks', function() { arr: [ 1, 2, 3, 3, 2, 1 ], expected: [ 1, 2, 3 ] }, { - arr: [ 'a', 'a', 'a', 'a', 'a' ], - expected: [ 'a' ] - }, { - arr: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], - expected: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] - }, { - arr: [ 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6 ], - expected: [ 1, 2, 3, 4, 5, 6 ] - } + arr: [ 'a', 'a', 'a', 'a', 'a' ], + expected: [ 'a' ] + }, { + arr: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], + expected: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] + }, { + arr: [ 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6 ], + expected: [ 1, 2, 3, 4, 5, 6 ] + } ].forEach(data => { var actual = tasks.distinct(data.arr); assert.deepEqual( @@ -730,38 +730,38 @@ describe('04-arrays-tasks', function() { [ { arr: [ - { country: 'Belarus', city: 'Brest' }, - { country: 'Russia', city: 'Omsk' }, - { country: 'Russia', city: 'Samara' }, - { country: 'Belarus', city: 'Grodno' }, - { country: 'Belarus', city: 'Minsk' }, - { country: 'Poland', city: 'Lodz' } + { country: 'Belarus', city: 'Brest' }, + { country: 'Russia', city: 'Omsk' }, + { country: 'Russia', city: 'Samara' }, + { country: 'Belarus', city: 'Grodno' }, + { country: 'Belarus', city: 'Minsk' }, + { country: 'Poland', city: 'Lodz' } ], keySelector: item => item.country, valueSelector: item => item.city, expected: new Map([ - ['Belarus', ['Brest', 'Grodno', 'Minsk']], - ['Russia', ['Omsk', 'Samara']], - ['Poland', ['Lodz']] + ['Belarus', ['Brest', 'Grodno', 'Minsk']], + ['Russia', ['Omsk', 'Samara']], + ['Poland', ['Lodz']] ]) }, { - arr: [ - { artist: 'ACDC', album: 'Highway to Hell' }, - { artist: 'Metallica', album: "Kill'em All" }, - { artist: 'Deep Purple', album: 'Machine Head' }, - { artist: 'Metallica', album: 'And Justice for All' }, - { artist: 'ACDC', album: 'Back in Black' }, - { artist: 'Manowar', album: 'Kings of Metal' } - ], - keySelector: item => item.artist, - valueSelector: item => item.album, - expected: new Map([ - ['ACDC', ['Highway to Hell', 'Back in Black']], - ['Metallica', ["Kill'em All", 'And Justice for All']], - ['Deep Purple', ['Machine Head']], - ['Manowar',['Kings of Metal']] - ]) - } + arr: [ + { artist: 'ACDC', album: 'Highway to Hell' }, + { artist: 'Metallica', album: "Kill'em All" }, + { artist: 'Deep Purple', album: 'Machine Head' }, + { artist: 'Metallica', album: 'And Justice for All' }, + { artist: 'ACDC', album: 'Back in Black' }, + { artist: 'Manowar', album: 'Kings of Metal' } + ], + keySelector: item => item.artist, + valueSelector: item => item.album, + expected: new Map([ + ['ACDC', ['Highway to Hell', 'Back in Black']], + ['Metallica', ["Kill'em All", 'And Justice for All']], + ['Deep Purple', ['Machine Head']], + ['Manowar',['Kings of Metal']] + ]) + } ].forEach(data => { var actual = tasks.group(data.arr, data.keySelector, data.valueSelector); assert.deepEqual( @@ -779,14 +779,14 @@ describe('04-arrays-tasks', function() { childrenSelector : x => x, expected: [ 1, 2, 3, 4, 5, 6 ] }, { - arr: [[11, 12, 13, 14, 15], [21, 22, ,23, 24, 25], [31, 32, 34, 35]], - childrenSelector : x => x.slice(0,2), - expected: [ 11, 12, 21, 22, 31, 32 ] - }, { - arr: ['one','two','three'], - childrenSelector: x=>x.split(''), - expected: ['o','n','e','t','w','o','t','h','r','e','e'] - } + arr: [[11, 12, 13, 14, 15], [21, 22, ,23, 24, 25], [31, 32, 34, 35]], + childrenSelector : x => x.slice(0,2), + expected: [ 11, 12, 21, 22, 31, 32 ] + }, { + arr: ['one','two','three'], + childrenSelector: x=>x.split(''), + expected: ['o','n','e','t','w','o','t','h','r','e','e'] + } ].forEach(data => { var actual = tasks.selectMany(data.arr, data.childrenSelector); assert.deepStrictEqual( @@ -804,14 +804,14 @@ describe('04-arrays-tasks', function() { indexes: [ 0, 0 ], expected: 1 }, { - arr: ['one','two','three'], - indexes: [ 2 ], - expected: 'three' - }, { - arr: [[[1,2,3]]], - indexes: [ 0, 0, 1 ], - expected: 2 - } + arr: ['one','two','three'], + indexes: [ 2 ], + expected: 'three' + }, { + arr: [[[1,2,3]]], + indexes: [ 0, 0, 1 ], + expected: 2 + } ].forEach(data => { var actual = tasks.getElementByIndexes(data.arr, data.indexes); assert.equal( @@ -820,7 +820,7 @@ describe('04-arrays-tasks', function() { `getElementByIndexes(${JSON.stringify(data.arr)}, ${JSON.stringify(data.indexes)}) returns an incorrect result. Expected ${data.expected}, but actual ${actual}` ); }); - }); + }); it.optional('swapHeadAndTail should swap the head and tail of the array', function () { @@ -829,38 +829,38 @@ describe('04-arrays-tasks', function() { arr: [ 1 ], expected: [ 1 ] },{ - arr: [ 1, 2 ], - expected: [ 2, 1 ] - },{ - arr: [ 1, 2, 3 ], - expected: [ 3, 2, 1 ] - },{ - arr: [ 1, 2, 3, 4 ], - expected: [ 3, 4, 1, 2 ] - },{ - arr: [ 1, 2, 3, 4, 5 ], - expected: [ 4, 5, 3, 1, 2 ] - } + arr: [ 1, 2 ], + expected: [ 2, 1 ] + },{ + arr: [ 1, 2, 3 ], + expected: [ 3, 2, 1 ] + },{ + arr: [ 1, 2, 3, 4 ], + expected: [ 3, 4, 1, 2 ] + },{ + arr: [ 1, 2, 3, 4, 5 ], + expected: [ 4, 5, 3, 1, 2 ] + } ].forEach(data => { var actual = tasks.swapHeadAndTail(Array.from(data.arr)); assert.deepEqual( data.expected, actual, `The result of swaping head and tail [${data.arr}] is not correct` - ); + ) ; }); }); it.optional('Functions from 04-array-test.js should not use basic loops statements', function () { Object.getOwnPropertyNames(tasks) - .filter(x => tasks[x] instanceof Function) - .forEach(f => { - assert( - !/([;{]\s*(for|while)\s*\()|(\.forEach\s*\()/.test(tasks[f].toString()), - `Function "${f}" should not use basic loop statements (for, while or Array.forEach)! Please use specialized array methods (Array.map, Array.reduce etc).` - ); - }); - }); - + .filter(x => tasks[x] instanceof Function) + .forEach(f => { + assert( + !/([;{]\s*(for|while)\s*\()|(\.forEach\s*\()/.test(tasks[f].toString()), + `Function "${f}" should not use basic loop statements (for, while or Array.forEach)! Please use specialized array methods (Array.map, Array.reduce etc).` + ); + }); + }); + }); diff --git a/test/05-regex-tests.js b/test/05-regex-tests.js index 7e789f8a1..5a95b24b0 100644 --- a/test/05-regex-tests.js +++ b/test/05-regex-tests.js @@ -28,10 +28,10 @@ describe('05-regex-tasks', function() { '0c74f13f-fa83-4c48-9b33-68921dd72463', 'The roof, the roof, the roof is on fire' ].forEach((str) => { - assert( - result.test(str) == false, + assert( + result.test(str) == false, `regex matches '${str}'` - ); + ); }); }); @@ -154,8 +154,8 @@ describe('05-regex-tasks', function() { }); assert( - !'abcdABCD1234'.match(tasks.getPasswordValidator(20)), - 'Password validator do not validate minLength restriction' + !'abcdABCD1234'.match(tasks.getPasswordValidator(20)), + 'Password validator do not validate minLength restriction' ); }); }); diff --git a/test/06-conditions-n-loops-tests.js b/test/06-conditions-n-loops-tests.js index 93ffa3eee..e8bcb2ca9 100644 --- a/test/06-conditions-n-loops-tests.js +++ b/test/06-conditions-n-loops-tests.js @@ -9,8 +9,8 @@ describe('06-conditions-n-loops-tasks', function() { it.optional('getFizzBuzz should return the output value according specification', () => { [ 1, 2, 4, 7, 8, 11, 13, 14, 16, 17, 19, 22, 23, 26, 28, 29, 31, 32, 34, - 37, 38, 41, 43, 44, 47, 49, 52, 53, 56, 58, 59, 61, 62, 64, 67, 68, 71, - 73, 74, 76, 77, 79, 82, 83, 86, 88, 89, 91, 92, 94, 97, 98 + 37, 38, 41, 43, 44, 47, 49, 52, 53, 56, 58, 59, 61, 62, 64, 67, 68, 71, + 73, 74, 76, 77, 79, 82, 83, 86, 88, 89, 91, 92, 94, 97, 98 ].forEach(num => { var actual = tasks.getFizzBuzz(num); assert.equal( @@ -22,9 +22,9 @@ describe('06-conditions-n-loops-tasks', function() { [ 3, 6, 9, 12, 18, 21, 24, 27, - 33, 36, 39, 42, 48, 51, 54, 57, - 63, 66, 69, 72, 78, 81, 84, 87, - 93, 96, 99 + 33, 36, 39, 42, 48, 51, 54, 57, + 63, 66, 69, 72, 78, 81, 84, 87, + 93, 96, 99 ].forEach(num => { var actual = tasks.getFizzBuzz(num); assert.equal( @@ -97,7 +97,7 @@ describe('06-conditions-n-loops-tasks', function() { { sides: [ 3, 4, 5] , expected: true }, { sides: [ 10, 1, 1] , expected: false }, { sides: [ 10, 10, 10] , expected: true }, - ].forEach(data => { + ].forEach(data => { [[0,1,2], [0,2,1], [1,2,0], [1,0,2], [2,0,1], [2,1,0]].forEach(idx => { var actual = tasks.isTriangle( data.sides[idx[0]], @@ -116,36 +116,36 @@ describe('06-conditions-n-loops-tasks', function() { it.optional('doRectanglesOverlap should return true if rectangles overlap', () => { [ - { + { rect1: { top: 0, left: 0, width: 10, height: 10 }, rect2: { top: 5, left: 5, width: 10, height: 10 }, expected: true },{ - rect1: { top: 10, left: 10, width: 10, height: 10 }, - rect2: { top: 5, left: 5, width: 15, height: 15 }, - expected: true - },{ - rect1: { top: 10, left: 10, width: 50, height: 5 }, - rect2: { top: 5, left: 5, width: 10, height: 50 }, - expected: true - },{ - rect1: { top: 0, left: 0, width: 90, height: 90 }, - rect2: { top: 25, left: 25, width: 10, height: 10 }, - expected: true - },{ - rect1: { top: 5, left: 5, width: 20, height: 20 }, - rect2: { top: 5, left: 5, width: 40, height: 10 }, - expected: true - },{ - rect1: { top: 5, left: 5, width: 20, height: 20 }, - rect2: { top: 30, left: 5, width: 40, height: 10 }, - expected: false - },{ - rect1: { top: 0, left: 0, width: 90, height: 90 }, - rect2: { top: 25, left:100, width: 10, height: 10 }, - expected: false - } - ].forEach(data => { + rect1: { top: 10, left: 10, width: 10, height: 10 }, + rect2: { top: 5, left: 5, width: 15, height: 15 }, + expected: true + },{ + rect1: { top: 10, left: 10, width: 50, height: 5 }, + rect2: { top: 5, left: 5, width: 10, height: 50 }, + expected: true + },{ + rect1: { top: 0, left: 0, width: 90, height: 90 }, + rect2: { top: 25, left: 25, width: 10, height: 10 }, + expected: true + },{ + rect1: { top: 5, left: 5, width: 20, height: 20 }, + rect2: { top: 5, left: 5, width: 40, height: 10 }, + expected: true + },{ + rect1: { top: 5, left: 5, width: 20, height: 20 }, + rect2: { top: 30, left: 5, width: 40, height: 10 }, + expected: false + },{ + rect1: { top: 0, left: 0, width: 90, height: 90 }, + rect2: { top: 25, left:100, width: 10, height: 10 }, + expected: false + } + ].forEach(data => { assert.equal( tasks.doRectanglesOverlap(data.rect1, data.rect2), data.expected, @@ -157,36 +157,36 @@ describe('06-conditions-n-loops-tasks', function() { it.optional('isInsideCircle should return true if point lies inside of the specified circle', () => { [ - { + { circle: { center: { x: 0, y: 0 }, radius: 10 }, point: { x: 0, y: 0 }, expected: true - },{ - circle: { center: { x: 5, y: 5 }, radius: 6 }, - point: { x: 5, y: 10.99 }, - expected: true - },{ - circle: { center: { x: 0, y: 0 }, radius: 10 }, - point: { x: 0, y: 10 }, - expected: false - },{ - circle: { center: { x: 5, y: 5 }, radius: 6 }, - point: { x: 0, y: 0 }, - expected: false - },{ - circle: { center: { x: 2, y: 2 }, radius: 1 }, - point: { x: 2.8, y: 2.8 }, - expected: false - },{ - circle: { center: { x: 2, y: 2 }, radius: 4 }, - point: { x: -1, y: -1 }, - expected: false },{ - circle: { center: { x: 2, y: 2 }, radius: 4 }, - point: { x: 2, y: 6.1 }, - expected: false - } - ].forEach(data => { + circle: { center: { x: 5, y: 5 }, radius: 6 }, + point: { x: 5, y: 10.99 }, + expected: true + },{ + circle: { center: { x: 0, y: 0 }, radius: 10 }, + point: { x: 0, y: 10 }, + expected: false + },{ + circle: { center: { x: 5, y: 5 }, radius: 6 }, + point: { x: 0, y: 0 }, + expected: false + },{ + circle: { center: { x: 2, y: 2 }, radius: 1 }, + point: { x: 2.8, y: 2.8 }, + expected: false + },{ + circle: { center: { x: 2, y: 2 }, radius: 4 }, + point: { x: -1, y: -1 }, + expected: false + },{ + circle: { center: { x: 2, y: 2 }, radius: 4 }, + point: { x: 2, y: 6.1 }, + expected: false + } + ].forEach(data => { assert.equal( tasks.isInsideCircle(data.circle, data.point), data.expected, @@ -221,30 +221,30 @@ describe('06-conditions-n-loops-tasks', function() { isEndIncluded: true, expected: '[0, 1]' },{ - a: 0, - b: 1, - isStartIncluded: true, - isEndIncluded: false, - expected: '[0, 1)' - },{ - a: 0, - b: 1, - isStartIncluded: false, - isEndIncluded: true, - expected: '(0, 1]' - },{ - a: 0, - b: 1, - isStartIncluded: false, - isEndIncluded: false, - expected: '(0, 1)' - },{ - a: 5, - b: 3, - isStartIncluded: true, - isEndIncluded: true, - expected: '[3, 5]' - } + a: 0, + b: 1, + isStartIncluded: true, + isEndIncluded: false, + expected: '[0, 1)' + },{ + a: 0, + b: 1, + isStartIncluded: false, + isEndIncluded: true, + expected: '(0, 1]' + },{ + a: 0, + b: 1, + isStartIncluded: false, + isEndIncluded: false, + expected: '(0, 1)' + },{ + a: 5, + b: 3, + isStartIncluded: true, + isEndIncluded: true, + expected: '[3, 5]' + } ].forEach(data => { var actual = tasks.getIntervalString(data.a, data.b, data.isStartIncluded, data.isEndIncluded); assert.equal( @@ -354,7 +354,7 @@ describe('06-conditions-n-loops-tasks', function() { }); - it.optional('isBracketsBalanced should check the balanced brackets', () => { + it.optional('isBracketsBalanced should check the balanced brackets', () => { [ '[]', '[[][][[]]]', '[[][]]', '', '<>', '{}', '()', '<()>', '{<>}', '[{}]', '[{(<()[]{}<>>)}]', '{}<>()[]','{<>}{()}[[]](())' @@ -408,15 +408,15 @@ describe('06-conditions-n-loops-tasks', function() { pathes: ['/web/images/image1.png', '/web/images/image2.png'], expected: '/web/images/' },{ - pathes: ['/web/assets/style.css', '/web/scripts/app.js', 'home/setting.conf'], - expected: '' - },{ - pathes: ['/web/assets/style.css', '/.bin/mocha', '/read.me'], - expected: '/' - },{ - pathes: ['/web/favicon.ico', '/web-scripts/dump', '/webalizer/logs'], - expected: '/' - } + pathes: ['/web/assets/style.css', '/web/scripts/app.js', 'home/setting.conf'], + expected: '' + },{ + pathes: ['/web/assets/style.css', '/.bin/mocha', '/read.me'], + expected: '/' + },{ + pathes: ['/web/favicon.ico', '/web-scripts/dump', '/webalizer/logs'], + expected: '/' + } ].forEach(data => { var actual = tasks.getCommonDirectoryPath(data.pathes, data.n); assert.equal( @@ -447,16 +447,16 @@ describe('06-conditions-n-loops-tasks', function() { [ 7, 8, 9 ] ] },{ - m1: [ - [ 1, 2, 3] - ], - m2: [ - [ 4 ], - [ 5 ], - [ 6 ] - ], - expected : [[ 32 ]] - } + m1: [ + [ 1, 2, 3] + ], + m2: [ + [ 4 ], + [ 5 ], + [ 6 ] + ], + expected : [[ 32 ]] + } ].forEach(data => { var actual = tasks.getMatrixProduct(data.m1, data.m2); assert.deepEqual( @@ -475,130 +475,130 @@ describe('06-conditions-n-loops-tasks', function() { endDate: '2000-01-01 01:00:00.200', expected: 'a few seconds ago' }, { - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 01:00:30.000', - expected: 'a few seconds ago' - }, { - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 01:00:45.000', - expected: 'a few seconds ago' - }, { - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 01:00:45.001', - expected: 'a minute ago' - }, { - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 01:01:00.000', - expected: 'a minute ago' - }, { - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 01:01:30.000', - expected: 'a minute ago' - }, { - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 01:01:30.001', - expected: '2 minutes ago' - }, { - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 01:05:30.000', - expected: '5 minutes ago' - },{ - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 01:45:00.000', - expected: '45 minutes ago' - },{ - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 01:45:00.001', - expected: 'an hour ago' - },{ - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 02:00:00.000', - expected: 'an hour ago' - },{ - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 02:30:00.000', - expected: 'an hour ago' - },{ - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 02:30:00.001', - expected: '2 hours ago' - },{ - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 05:30:00.000', - expected: '4 hours ago' - },{ - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 05:30:00.001', - expected: '5 hours ago' - },{ - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 23:00:00.000', - expected: '22 hours ago' - },{ - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-01 23:00:00.001', - expected: 'a day ago' - },{ - startDate: '2000-01-01 01:00:00.000', - endDate: '2000-01-02 01:00:00.000', - expected: 'a day ago' - },{ - startDate: '2000-01-01 00:00:00.000', - endDate: '2000-01-02 12:00:00.000', - expected: 'a day ago' - },{ - startDate: '2000-01-01 00:00:00.000', - endDate: '2000-01-02 12:00:00.001', - expected: '2 days ago' - },{ - startDate: '2000-01-01 00:00:00.000', - endDate: '2000-01-05 12:00:00.000', - expected: '4 days ago' - },{ - startDate: '2000-01-01 00:00:00.000', - endDate: '2000-01-26 00:00:00.000', - expected: '25 days ago' - },{ - startDate: '2000-01-01 00:00:00.000', - endDate: '2000-01-26 00:00:00.001', - expected: 'a month ago' - },{ - startDate: '2000-01-01 00:00:00.000', - endDate: '2000-02-01 00:00:00.000', - expected: 'a month ago' - },{ - startDate: '2000-01-01 00:00:00.000', - endDate: '2000-02-15 00:00:00.000', - expected: 'a month ago' - },{ - startDate: '2000-01-01 00:00:00.000', - endDate: '2000-02-16 00:00:00.000', - expected: '2 months ago' - },{ - startDate: '2000-01-01 00:00:00.000', - endDate: '2000-05-20 00:00:00.000', - expected: '5 months ago' - },{ - startDate: '2000-01-01 00:00:00.000', - endDate: '2000-12-10 00:00:00.000', - expected: '11 months ago' - },{ - startDate: '2000-01-01 00:00:00.000', - endDate: '2000-12-12 00:00:00.000', - expected: 'a year ago' - },{ - startDate: '2000-01-01 00:00:00.000', - endDate: '2001-02-15 00:00:00.001', - expected: 'a year ago' - },{ - startDate: '2000-01-01 00:00:00.000', - endDate: '2001-06-01 00:00:00.001', - expected: 'a year ago' - },{ - startDate: '2000-01-01 00:00:00.000', - endDate: '2015-02-15 00:00:00.001', - expected: '15 years ago' - } + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 01:00:30.000', + expected: 'a few seconds ago' + }, { + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 01:00:45.000', + expected: 'a few seconds ago' + }, { + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 01:00:45.001', + expected: 'a minute ago' + }, { + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 01:01:00.000', + expected: 'a minute ago' + }, { + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 01:01:30.000', + expected: 'a minute ago' + }, { + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 01:01:30.001', + expected: '2 minutes ago' + }, { + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 01:05:30.000', + expected: '5 minutes ago' + },{ + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 01:45:00.000', + expected: '45 minutes ago' + },{ + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 01:45:00.001', + expected: 'an hour ago' + },{ + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 02:00:00.000', + expected: 'an hour ago' + },{ + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 02:30:00.000', + expected: 'an hour ago' + },{ + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 02:30:00.001', + expected: '2 hours ago' + },{ + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 05:30:00.000', + expected: '4 hours ago' + },{ + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 05:30:00.001', + expected: '5 hours ago' + },{ + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 23:00:00.000', + expected: '22 hours ago' + },{ + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-01 23:00:00.001', + expected: 'a day ago' + },{ + startDate: '2000-01-01 01:00:00.000', + endDate: '2000-01-02 01:00:00.000', + expected: 'a day ago' + },{ + startDate: '2000-01-01 00:00:00.000', + endDate: '2000-01-02 12:00:00.000', + expected: 'a day ago' + },{ + startDate: '2000-01-01 00:00:00.000', + endDate: '2000-01-02 12:00:00.001', + expected: '2 days ago' + },{ + startDate: '2000-01-01 00:00:00.000', + endDate: '2000-01-05 12:00:00.000', + expected: '4 days ago' + },{ + startDate: '2000-01-01 00:00:00.000', + endDate: '2000-01-26 00:00:00.000', + expected: '25 days ago' + },{ + startDate: '2000-01-01 00:00:00.000', + endDate: '2000-01-26 00:00:00.001', + expected: 'a month ago' + },{ + startDate: '2000-01-01 00:00:00.000', + endDate: '2000-02-01 00:00:00.000', + expected: 'a month ago' + },{ + startDate: '2000-01-01 00:00:00.000', + endDate: '2000-02-15 00:00:00.000', + expected: 'a month ago' + },{ + startDate: '2000-01-01 00:00:00.000', + endDate: '2000-02-16 00:00:00.000', + expected: '2 months ago' + },{ + startDate: '2000-01-01 00:00:00.000', + endDate: '2000-05-20 00:00:00.000', + expected: '5 months ago' + },{ + startDate: '2000-01-01 00:00:00.000', + endDate: '2000-12-10 00:00:00.000', + expected: '11 months ago' + },{ + startDate: '2000-01-01 00:00:00.000', + endDate: '2000-12-12 00:00:00.000', + expected: 'a year ago' + },{ + startDate: '2000-01-01 00:00:00.000', + endDate: '2001-02-15 00:00:00.001', + expected: 'a year ago' + },{ + startDate: '2000-01-01 00:00:00.000', + endDate: '2001-06-01 00:00:00.001', + expected: 'a year ago' + },{ + startDate: '2000-01-01 00:00:00.000', + endDate: '2015-02-15 00:00:00.001', + expected: '15 years ago' + } ].forEach(data => { var actual = tasks.timespanToHumanString(new Date(data.startDate), new Date(data.endDate)); assert.equal( @@ -630,35 +630,35 @@ describe('06-conditions-n-loops-tasks', function() { [ X, X, X ], [ O, O, ], [ O, , ] - ],[ + ],[ [ , O, O ], [ X, X, X ], [ O, , O ] - ],[ + ],[ [ , , O ], [ O, , O ], [ X, X, X ] - ],[ + ],[ [ X, , O ], [ X, , O ], [ X, O, ] - ],[ + ],[ [ O, X, O ], [ X, X, O ], [ O, X, ] - ],[ + ],[ [ O, O, X ], [ X, O, X ], [ O, X, X ] - ],[ + ],[ [ X, O, O ], [ X, X, O ], [ O, X, X ] - ],[ + ],[ [ O, O, X ], [ X, X, O ], [ X, , O ] - ] + ] ].forEach(data => { var actual = tasks.evaluateTicTacToePosition(data); assert.equal( @@ -672,35 +672,35 @@ describe('06-conditions-n-loops-tasks', function() { [ O, O, O ], [ , X, X ], [ X, , ] - ],[ + ],[ [ X, X, ], [ O, O, O ], [ X, , X ] - ],[ + ],[ [ , , ], [ X, , X ], [ O, O, O ] - ],[ + ],[ [ O, , X ], [ O, X, X ], [ O, X, ] - ],[ + ],[ [ X, O, X ], [ X, O, O ], [ O, O, X ] - ],[ + ],[ [ X, X, O ], [ X, O, O ], [ , X, O ] - ],[ + ],[ [ O, X, X ], [ X, O, X ], [ O, X, O ] - ],[ + ],[ [ X, X, O ], [ X, O, X ], [ O, , X ] - ] + ] ].forEach(data => { var actual = tasks.evaluateTicTacToePosition(data); assert.equal( @@ -714,23 +714,23 @@ describe('06-conditions-n-loops-tasks', function() { [ , , ], [ , , ], [ , , ] - ],[ + ],[ [ X, , ], [ O, O, ], [ , , X ] - ],[ + ],[ [ X, O, X ], [ X, O, X ], [ O, X, O ] - ],[ + ],[ [ X, O, X ], [ O, X, X ], [ O, X, O ] - ],[ + ],[ [ X, O, X ], [ O, , O ], [ X, O, X ] - ] + ] ].forEach(data => { var actual = tasks.evaluateTicTacToePosition(data); assert.equal( diff --git a/test/07-yield-tests.js b/test/07-yield-tests.js index a38c53e3d..82210f98e 100644 --- a/test/07-yield-tests.js +++ b/test/07-yield-tests.js @@ -252,17 +252,17 @@ describe('07-yield-tasks', function() { it.optional('depthTraversalTree should return the sequence of tree nodes in depth-first order', () => { - /* - * source tree (root = 1): - * - * 1 - * / | \ - * 2 6 7 - * / \ \ => { 1, 2, 3, 4, 5, 6, 7, 8 } - * 3 4 8 - * | - * 5 - */ + /* + * source tree (root = 1): + * + * 1 + * / | \ + * 2 6 7 + * / \ \ => { 1, 2, 3, 4, 5, 6, 7, 8 } + * 3 4 8 + * | + * 5 + */ var node1 = { n:1 }, node2 = { n:2 }, node3 = { n:3 }, node4 = { n:4 }, node5 = { n:5 }, node6 = { n:6 }, node7 = { n:7 }, node8 = { n:8 }; node1.children = [ node2, node6, node7 ]; @@ -333,17 +333,17 @@ describe('07-yield-tasks', function() { it.optional('breadthTraversalTree should return the sequence of tree nodes in depth-first order', () => { - /* - * source tree (root = 1): - * - * 1 - * / | \ - * 2 3 4 - * / \ \ => { 1, 2, 3, 4, 5, 6, 7, 8 } - * 5 6 7 - * | - * 8 - */ + /* + * source tree (root = 1): + * + * 1 + * / | \ + * 2 3 4 + * / \ \ => { 1, 2, 3, 4, 5, 6, 7, 8 } + * 5 6 7 + * | + * 8 + */ var node1 = { n:1 }, node2 = { n:2 }, node3 = { n:3 }, node4 = { n:4 }, node5 = { n:5 }, node6 = { n:6 }, node7 = { n:7 }, node8 = { n:8 }; node1.children = [ node2, node3, node4 ]; @@ -397,13 +397,13 @@ describe('07-yield-tasks', function() { it.optional('mergeSortedSequences should merge two sorted sequences into one sorted sequence', () => { const ITEMS_COUNT = 500; - + var odds = function* () { - for(var i=1; true; i+=2) yield i; - }; + for(var i=1; true; i+=2) yield i; + }; var evens = function* () { - for(var i=2; true; i+=2) yield i; - }; + for(var i=2; true; i+=2) yield i; + }; var expected = 1; var count = 0; for(let value of tasks.mergeSortedSequences(odds, evens)) { @@ -429,7 +429,7 @@ describe('07-yield-tasks', function() { if (count == ITEMS_COUNT) break; } assert.equal(count, ITEMS_COUNT); - + var minus1 = function* () { yield -1; } expected = -1; diff --git a/test/08-objects-tests.js b/test/08-objects-tests.js index 09366fa25..fe7bcdac2 100644 --- a/test/08-objects-tests.js +++ b/test/08-objects-tests.js @@ -56,9 +56,9 @@ describe('08-objects-tasks', function() { obj: [ 1, 2, 3], expected: '[1,2,3]' },{ - obj: { height: 10, width: 20 }, - expected: '{"height":10,"width":20}' - } + obj: { height: 10, width: 20 }, + expected: '{"height":10,"width":20}' + } ].forEach(data => { assert.equal( tasks.getJSON(data.obj), @@ -81,10 +81,10 @@ describe('08-objects-tasks', function() { json: '{ "width":10, "height":20 }', expected: new tasks.Rectangle(10, 20) },{ - proto: MockType.prototype, - json: '{ "a":10, "b":20, "c":30 }', - expected: new MockType(10,20,30) - } + proto: MockType.prototype, + json: '{ "a":10, "b":20, "c":30 }', + expected: new MockType(10,20,30) + } ].forEach(data => { var actual = tasks.fromJSON(data.proto, data.json); assert.deepEqual( @@ -231,7 +231,7 @@ describe('08-objects-tasks', function() { assert.throws( fn, /Element, id and pseudo-element should not occur more then one time inside the selector/, - + '\nPlease throw an exception "Element, id and pseudo-element should not occur more then one time inside the selector" '+ 'if element, id or pseudo-element occurs twice or more times' ); @@ -259,7 +259,7 @@ describe('08-objects-tasks', function() { assert.throws( fn, /Selector parts should be arranged in the following order: element, id, class, attribute, pseudo-class, pseudo-element/, - + '\nPlease throw an exception "Selector parts should be arranged in the following order: element, id, class, attribute, pseudo-class, pseudo-element" '+ 'if selector parts arranged in an invalid order.' ); diff --git a/test/09-functions-n-closures-tests.js b/test/09-functions-n-closures-tests.js index 595713a90..14ee1d59a 100644 --- a/test/09-functions-n-closures-tests.js +++ b/test/09-functions-n-closures-tests.js @@ -40,12 +40,12 @@ describe('09-functions-n-closures-tasks', function() { polynom: tasks.getPolynom(2,3,5), results: [ {x: 0, y: 5}, {x: 2, y: 19}, {x: 3, y: 32} ] },{ - polynom: tasks.getPolynom(1,-3), - results: [ {x:0, y: -3}, {x:2, y: -1}, {x:5, y:2} ] - },{ - polynom: tasks.getPolynom(8), - results: [ {x:0, y:8}, {x:2, y:8}, {x:5, y:8} ] - } + polynom: tasks.getPolynom(1,-3), + results: [ {x:0, y: -3}, {x:2, y: -1}, {x:5, y:2} ] + },{ + polynom: tasks.getPolynom(8), + results: [ {x:0, y:8}, {x:2, y:8}, {x:5, y:8} ] + } ].forEach(data => { data.results.forEach(test => { assert( @@ -66,9 +66,9 @@ describe('09-functions-n-closures-tasks', function() { var expected = memoizer(); assert.equal(numberOfCalls, 1, 'memoize result should evaluate the specified function at first call'); for(var i=0; i<10; i++) { - let actual = memoizer(); - assert.equal(actual, expected, 'memoize result should return the cached value at second and next calls'); - assert.equal(numberOfCalls, 1, 'memoize result should not evaluate the specified function at second and next calls'); + let actual = memoizer(); + assert.equal(actual, expected, 'memoize result should return the cached value at second and next calls'); + assert.equal(numberOfCalls, 1, 'memoize result should not evaluate the specified function at second and next calls'); } }); @@ -100,7 +100,7 @@ describe('09-functions-n-closures-tasks', function() { assert.equal( log, 'cos(3.141592653589793) starts\n' - +'cos(3.141592653589793) ends\n', + +'cos(3.141592653589793) ends\n', 'logger function shoud log the start and end of the specified function'); }); @@ -129,7 +129,7 @@ describe('09-functions-n-closures-tasks', function() { assert.equal( log, 'testLogger(["expected","test",1],0) starts\n' - +'testLogger(["expected","test",1],0) ends\n', + +'testLogger(["expected","test",1],0) ends\n', 'logger function shoud log the end of specified function after calling'); }); diff --git a/test/10-katas-1-tests.js b/test/10-katas-1-tests.js index 0d9023da6..16ade7b42 100644 --- a/test/10-katas-1-tests.js +++ b/test/10-katas-1-tests.js @@ -51,9 +51,9 @@ describe('10-katas-1-tasks', function() { ]; assert.deepEqual( - tasks.createCompassPoints(), - expected - ); + tasks.createCompassPoints(), + expected + ); }); @@ -61,38 +61,38 @@ describe('10-katas-1-tasks', function() { it.optional('expandBraces should expand the braces from pattern string', () => { [ { - str: '~/{Downloads,Pictures}/*.{jpg,gif,png}', - result : [ + str: '~/{Downloads,Pictures}/*.{jpg,gif,png}', + result : [ '~/Downloads/*.gif', '~/Downloads/*.jpg', '~/Downloads/*.png', '~/Pictures/*.gif', '~/Pictures/*.jpg', '~/Pictures/*.png' - ] + ] }, { - str: 'It{{em,alic}iz,erat}e{d,}, please.', - result : [ - 'Italicize, please.', - 'Italicized, please.', - 'Itemize, please.', - 'Itemized, please.', - 'Iterate, please.', - 'Iterated, please.' - ] - },{ - str: 'thumbnail.{png,jp{e,}g}', - result : [ - 'thumbnail.jpeg', - 'thumbnail.jpg', - 'thumbnail.png' - ] - },{ - str: 'nothing to do', - result : [ - 'nothing to do' - ] - } + str: 'It{{em,alic}iz,erat}e{d,}, please.', + result : [ + 'Italicize, please.', + 'Italicized, please.', + 'Itemize, please.', + 'Itemized, please.', + 'Iterate, please.', + 'Iterated, please.' + ] + },{ + str: 'thumbnail.{png,jp{e,}g}', + result : [ + 'thumbnail.jpeg', + 'thumbnail.jpg', + 'thumbnail.png' + ] + },{ + str: 'nothing to do', + result : [ + 'nothing to do' + ] + } ].forEach(data => { var actual = Array.from(tasks.expandBraces(data.str)); actual.sort(); @@ -107,43 +107,43 @@ describe('10-katas-1-tasks', function() { it.optional('getZigZagMatrix should create a square matrix with zigzag path', () => { [ - [ + [ [0] - ],[ - [ 0, 1 ], - [ 2, 3 ] - ],[ - [ 0, 1, 5 ], - [ 2, 4, 6 ], - [ 3, 7, 8 ] - ],[ - [ 0, 1, 5, 6 ], - [ 2, 4, 7, 12 ], - [ 3, 8, 11, 13 ], - [ 9, 10, 14, 15 ] - ],[ - [ 0, 1, 5, 6, 14 ], - [ 2, 4, 7, 13, 15 ], - [ 3, 8, 12, 16, 21 ], - [ 9, 11, 17, 20, 22 ], - [ 10, 18, 19, 23, 24 ], - ],[ - [ 0, 1, 5, 6, 14, 15 ], - [ 2, 4, 7, 13, 16, 25 ], - [ 3, 8, 12, 17, 24, 26 ], - [ 9, 11, 18, 23, 27, 32 ], - [ 10, 19, 22, 28, 31, 33 ], - [ 20, 21, 29, 30, 34, 35 ], - ],[ - [ 0, 1, 5, 6, 14, 15, 27 ], - [ 2, 4, 7, 13, 16, 26, 28 ], - [ 3, 8, 12, 17, 25, 29, 38 ], - [ 9, 11, 18, 24, 30, 37, 39 ], - [ 10, 19, 23, 31, 36, 40, 45 ], - [ 20, 22, 32, 35, 41, 44, 46 ], - [ 21, 33, 34, 42, 43, 47, 48 ], - ] - ].forEach(data => { + ],[ + [ 0, 1 ], + [ 2, 3 ] + ],[ + [ 0, 1, 5 ], + [ 2, 4, 6 ], + [ 3, 7, 8 ] + ],[ + [ 0, 1, 5, 6 ], + [ 2, 4, 7, 12 ], + [ 3, 8, 11, 13 ], + [ 9, 10, 14, 15 ] + ],[ + [ 0, 1, 5, 6, 14 ], + [ 2, 4, 7, 13, 15 ], + [ 3, 8, 12, 16, 21 ], + [ 9, 11, 17, 20, 22 ], + [ 10, 18, 19, 23, 24 ], + ],[ + [ 0, 1, 5, 6, 14, 15 ], + [ 2, 4, 7, 13, 16, 25 ], + [ 3, 8, 12, 17, 24, 26 ], + [ 9, 11, 18, 23, 27, 32 ], + [ 10, 19, 22, 28, 31, 33 ], + [ 20, 21, 29, 30, 34, 35 ], + ],[ + [ 0, 1, 5, 6, 14, 15, 27 ], + [ 2, 4, 7, 13, 16, 26, 28 ], + [ 3, 8, 12, 17, 25, 29, 38 ], + [ 9, 11, 18, 24, 30, 37, 39 ], + [ 10, 19, 23, 31, 36, 40, 45 ], + [ 20, 22, 32, 35, 41, 44, 46 ], + [ 21, 33, 34, 42, 43, 47, 48 ], + ] + ].forEach(data => { var actual = tasks.getZigZagMatrix(data.length); assert.deepEqual( actual, @@ -156,14 +156,14 @@ describe('10-katas-1-tasks', function() { it.optional('canDominoesMakeRow should answer if specified subset of dominoes can be arranged in a row', () => { [ - [ - [0,1], [1,1] - ],[ - [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] - ] - ].forEach(data => { + [ + [0,1], [1,1] + ],[ + [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] + ] + ].forEach(data => { var actual = tasks.canDominoesMakeRow(data); assert.equal( actual, @@ -174,14 +174,14 @@ describe('10-katas-1-tasks', function() { [ - [ - [0,1], [2,3] - ],[ - [1,1], [2,2], [1,5], [5,6], [6,3] - ],[ - [0,0], [0,1], [0,2], [0,3], [1,1], [1,2], [1,3], [2,2], [2,3], [3,3] - ] - ].forEach(data => { + [ + [0,1], [2,3] + ],[ + [1,1], [2,2], [1,5], [5,6], [6,3] + ],[ + [0,0], [0,1], [0,2], [0,3], [1,1], [1,2], [1,3], [2,2], [2,3], [3,3] + ] + ].forEach(data => { var actual = tasks.canDominoesMakeRow(data); assert.equal( actual, @@ -196,22 +196,22 @@ describe('10-katas-1-tasks', function() { it.optional('extractRanges should return string expression of ordered list of integers', () => { [ { - nums: [ 0, 1, 2, 3, 4, 5 ], - result: '0-5' - },{ - nums: [ 1, 4, 5 ], - result: '1,4,5' - },{ - nums: [ 0, 1, 2, 5, 7, 8, 9], - result: '0-2,5,7-9' - },{ - nums: [ 1, 2, 4, 5], - result: '1,2,4,5' + nums: [ 0, 1, 2, 3, 4, 5 ], + result: '0-5' },{ - nums: [ 0, 1, 2, 4, 6, 7, 8, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, - 25, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39 ], - result: '0-2,4,6-8,11,12,14-25,27-33,35-39' - }, + nums: [ 1, 4, 5 ], + result: '1,4,5' + },{ + nums: [ 0, 1, 2, 5, 7, 8, 9], + result: '0-2,5,7-9' + },{ + nums: [ 1, 2, 4, 5], + result: '1,2,4,5' + },{ + nums: [ 0, 1, 2, 4, 6, 7, 8, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39 ], + result: '0-2,4,6-8,11,12,14-25,27-33,35-39' + }, ].forEach(data => { var actual = tasks.extractRanges(data.nums); assert.equal( diff --git a/test/11-katas-2-tests.js b/test/11-katas-2-tests.js index f347a50e0..7da2283a4 100644 --- a/test/11-katas-2-tests.js +++ b/test/11-katas-2-tests.js @@ -10,71 +10,71 @@ describe('11-katas-2-tasks', function() { [ { text: ' _ _ _ _ _ _ _ _ _ \n'+ - '| || || || || || || || || |\n'+ - '|_||_||_||_||_||_||_||_||_|\n', + '| || || || || || || || || |\n'+ + '|_||_||_||_||_||_||_||_||_|\n', result: 0 },{ - text: ' \n'+ - ' | | | | | | | | |\n'+ - ' | | | | | | | | |\n', - result: 111111111, + text: ' \n'+ + ' | | | | | | | | |\n'+ + ' | | | | | | | | |\n', + result: 111111111, - },{ - text: ' _ _ _ _ _ _ _ _ _ \n'+ - ' _| _| _| _| _| _| _| _| _|\n'+ - '|_ |_ |_ |_ |_ |_ |_ |_ |_ \n', - result: 222222222 - },{ - text: ' _ _ _ _ _ _ _ _ _ \n'+ - ' _| _| _| _| _| _| _| _| _|\n'+ - ' _| _| _| _| _| _| _| _| _|\n', - result: 333333333 - },{ - text: ' \n'+ - '|_||_||_||_||_||_||_||_||_|\n'+ - ' | | | | | | | | |\n', - result: 444444444 - },{ - text: ' _ _ _ _ _ _ _ _ _ \n'+ - '|_ |_ |_ |_ |_ |_ |_ |_ |_ \n'+ - ' _| _| _| _| _| _| _| _| _|\n', - result: 555555555 - },{ - text: ' _ _ _ _ _ _ _ _ _ \n'+ - '|_ |_ |_ |_ |_ |_ |_ |_ |_ \n'+ - '|_||_||_||_||_||_||_||_||_|\n', - result: 666666666 - },{ - text: ' _ _ _ _ _ _ _ _ _ \n'+ - ' | | | | | | | | |\n'+ - ' | | | | | | | | |\n', - result: 777777777 - },{ - text: ' _ _ _ _ _ _ _ _ _ \n'+ - '|_||_||_||_||_||_||_||_||_|\n'+ - '|_||_||_||_||_||_||_||_||_|\n', - result: 888888888 - },{ - text: ' _ _ _ _ _ _ _ _ _ \n'+ - '|_||_||_||_||_||_||_||_||_|\n'+ - ' _| _| _| _| _| _| _| _| _|\n', - result: 999999999 - },{ - text: ' _ _ _ _ _ _ _ \n'+ - ' | _| _||_||_ |_ ||_||_|\n'+ - ' ||_ _| | _||_| ||_| _|\n', - result: 123456789 - },{ - text: ' _ _ _ _ _ _ _ _ _ \n'+ - '| | _| _|| ||_ |_ ||_||_|\n'+ - '|_||_ _||_| _||_| ||_| _|\n', - result: 23056789 - },{ - text: ' _ _ _ _ _ _ _ _ _ \n'+ - '|_| _| _||_||_ |_ |_||_||_|\n'+ - '|_||_ _||_| _||_| _||_| _|\n', - result: 823856989 - } + },{ + text: ' _ _ _ _ _ _ _ _ _ \n'+ + ' _| _| _| _| _| _| _| _| _|\n'+ + '|_ |_ |_ |_ |_ |_ |_ |_ |_ \n', + result: 222222222 + },{ + text: ' _ _ _ _ _ _ _ _ _ \n'+ + ' _| _| _| _| _| _| _| _| _|\n'+ + ' _| _| _| _| _| _| _| _| _|\n', + result: 333333333 + },{ + text: ' \n'+ + '|_||_||_||_||_||_||_||_||_|\n'+ + ' | | | | | | | | |\n', + result: 444444444 + },{ + text: ' _ _ _ _ _ _ _ _ _ \n'+ + '|_ |_ |_ |_ |_ |_ |_ |_ |_ \n'+ + ' _| _| _| _| _| _| _| _| _|\n', + result: 555555555 + },{ + text: ' _ _ _ _ _ _ _ _ _ \n'+ + '|_ |_ |_ |_ |_ |_ |_ |_ |_ \n'+ + '|_||_||_||_||_||_||_||_||_|\n', + result: 666666666 + },{ + text: ' _ _ _ _ _ _ _ _ _ \n'+ + ' | | | | | | | | |\n'+ + ' | | | | | | | | |\n', + result: 777777777 + },{ + text: ' _ _ _ _ _ _ _ _ _ \n'+ + '|_||_||_||_||_||_||_||_||_|\n'+ + '|_||_||_||_||_||_||_||_||_|\n', + result: 888888888 + },{ + text: ' _ _ _ _ _ _ _ _ _ \n'+ + '|_||_||_||_||_||_||_||_||_|\n'+ + ' _| _| _| _| _| _| _| _| _|\n', + result: 999999999 + },{ + text: ' _ _ _ _ _ _ _ \n'+ + ' | _| _||_||_ |_ ||_||_|\n'+ + ' ||_ _| | _||_| ||_| _|\n', + result: 123456789 + },{ + text: ' _ _ _ _ _ _ _ _ _ \n'+ + '| | _| _|| ||_ |_ ||_||_|\n'+ + '|_||_ _||_| _||_| ||_| _|\n', + result: 23056789 + },{ + text: ' _ _ _ _ _ _ _ _ _ \n'+ + '|_| _| _||_||_ |_ |_||_||_|\n'+ + '|_||_ _||_| _||_| _||_| _|\n', + result: 823856989 + } ].forEach(data => { assert.equal( tasks.parseBankAccount(data.text), @@ -97,21 +97,21 @@ describe('11-katas-2-tasks', function() { 'characters.' ] },{ - cols: 12, - expected: [ - 'The String', - 'global', - 'object is a', - 'constructor', - 'for strings,', - 'or a', - 'sequence of', - 'characters.' - ] - },{ - cols: Number.MAX_SAFE_INTEGER, - expected: [ text ] - } + cols: 12, + expected: [ + 'The String', + 'global', + 'object is a', + 'constructor', + 'for strings,', + 'or a', + 'sequence of', + 'characters.' + ] + },{ + cols: Number.MAX_SAFE_INTEGER, + expected: [ text ] + } ].forEach(data => { assert.deepEqual( Array.from(tasks.wrapText(text, data.cols)), @@ -140,93 +140,93 @@ describe('11-katas-2-tasks', function() { hand: [ '4♥','5♥','6♥','7♥','8♥' ], expected: PokerRank.StraightFlush },{ - hand: [ 'A♣','K♣','Q♣','J♣','10♣' ], - expected: PokerRank.StraightFlush - },{ - hand: [ '10♦','9♦','6♦','7♦','8♦' ], - expected: PokerRank.StraightFlush - },{ - hand: [ 'A♠','4♠','3♠','5♠','2♠' ], - expected: PokerRank.StraightFlush - },{ - hand: [ '4♣','4♦','4♥','4♠','10♥' ], - expected: PokerRank.FourOfKind - },{ - hand: [ '2♣','A♦','A♣','A♠','A♥' ], - expected: PokerRank.FourOfKind - },{ - hand: [ '10♣','10♦','6♦','10♠','10♥' ], - expected: PokerRank.FourOfKind - },{ - hand: [ '4♣','4♦','5♦','5♠','5♥' ], - expected: PokerRank.FullHouse - },{ - hand: [ 'A♣','2♦','A♦','2♠','2♥' ], - expected: PokerRank.FullHouse - },{ - hand: [ '4♣','4♦','5♦','5♠','5♥' ], - expected: PokerRank.FullHouse - },{ - hand: [ '4♣','5♣','6♣','7♣','Q♣' ], - expected: PokerRank.Flush - },{ - hand: [ 'A♦','2♦','3♦','4♦','K♦' ], - expected: PokerRank.Flush - },{ - hand: [ 'A♠','Q♠','J♠','10♠','9♠' ], - expected: PokerRank.Flush - },{ - hand: [ '2♥','4♥','5♥','7♥','A♥' ], - expected: PokerRank.Flush - },{ - hand: [ '2♠','3♥','4♥','5♥','6♥' ], - expected: PokerRank.Straight - },{ - hand: [ 'A♠','K♦','Q♦','J♦','10♦' ], - expected: PokerRank.Straight - },{ - hand: [ '10♥','8♥','9♠','7♥','6♦' ], - expected: PokerRank.Straight - },{ - hand: [ '2♥','4♦','5♥','A♦','3♠' ], - expected: PokerRank.Straight - },{ - hand: [ '2♥','2♠','2♦','7♥','A♥' ], - expected: PokerRank.ThreeOfKind - },{ - hand: [ '2♥','4♥','A♥','A♦','A♠' ], - expected: PokerRank.ThreeOfKind - },{ - hand: [ '10♥','9♥','10♦','J♥','10♠' ], - expected: PokerRank.ThreeOfKind - },{ - hand: [ '2♥','4♦','4♥','A♦','A♠' ], - expected: PokerRank.TwoPairs - },{ - hand: [ '3♥','4♥','A♥','3♦','A♠' ], - expected: PokerRank.TwoPairs - },{ - hand: [ '5♥','6♥','A♥','6♦','5♠' ], - expected: PokerRank.TwoPairs - },{ - hand: [ '2♥','4♦','5♥','A♦','A♠' ], - expected: PokerRank.OnePair - },{ - hand: [ '3♥','4♥','10♥','3♦','A♠' ], - expected: PokerRank.OnePair - },{ - hand: [ '5♥','6♥','7♥','8♦','5♠' ], - expected: PokerRank.OnePair - },{ - hand: [ '3♥','4♥','5♥','7♦','8♥' ], - expected: PokerRank.HighCard - },{ - hand: [ 'A♥','K♥','Q♥','J♦','5♠' ], - expected: PokerRank.HighCard - },{ - hand: [ 'A♥','K♥','Q♥','2♦','3♠' ], - expected: PokerRank.HighCard - } + hand: [ 'A♣','K♣','Q♣','J♣','10♣' ], + expected: PokerRank.StraightFlush + },{ + hand: [ '10♦','9♦','6♦','7♦','8♦' ], + expected: PokerRank.StraightFlush + },{ + hand: [ 'A♠','4♠','3♠','5♠','2♠' ], + expected: PokerRank.StraightFlush + },{ + hand: [ '4♣','4♦','4♥','4♠','10♥' ], + expected: PokerRank.FourOfKind + },{ + hand: [ '2♣','A♦','A♣','A♠','A♥' ], + expected: PokerRank.FourOfKind + },{ + hand: [ '10♣','10♦','6♦','10♠','10♥' ], + expected: PokerRank.FourOfKind + },{ + hand: [ '4♣','4♦','5♦','5♠','5♥' ], + expected: PokerRank.FullHouse + },{ + hand: [ 'A♣','2♦','A♦','2♠','2♥' ], + expected: PokerRank.FullHouse + },{ + hand: [ '4♣','4♦','5♦','5♠','5♥' ], + expected: PokerRank.FullHouse + },{ + hand: [ '4♣','5♣','6♣','7♣','Q♣' ], + expected: PokerRank.Flush + },{ + hand: [ 'A♦','2♦','3♦','4♦','K♦' ], + expected: PokerRank.Flush + },{ + hand: [ 'A♠','Q♠','J♠','10♠','9♠' ], + expected: PokerRank.Flush + },{ + hand: [ '2♥','4♥','5♥','7♥','A♥' ], + expected: PokerRank.Flush + },{ + hand: [ '2♠','3♥','4♥','5♥','6♥' ], + expected: PokerRank.Straight + },{ + hand: [ 'A♠','K♦','Q♦','J♦','10♦' ], + expected: PokerRank.Straight + },{ + hand: [ '10♥','8♥','9♠','7♥','6♦' ], + expected: PokerRank.Straight + },{ + hand: [ '2♥','4♦','5♥','A♦','3♠' ], + expected: PokerRank.Straight + },{ + hand: [ '2♥','2♠','2♦','7♥','A♥' ], + expected: PokerRank.ThreeOfKind + },{ + hand: [ '2♥','4♥','A♥','A♦','A♠' ], + expected: PokerRank.ThreeOfKind + },{ + hand: [ '10♥','9♥','10♦','J♥','10♠' ], + expected: PokerRank.ThreeOfKind + },{ + hand: [ '2♥','4♦','4♥','A♦','A♠' ], + expected: PokerRank.TwoPairs + },{ + hand: [ '3♥','4♥','A♥','3♦','A♠' ], + expected: PokerRank.TwoPairs + },{ + hand: [ '5♥','6♥','A♥','6♦','5♠' ], + expected: PokerRank.TwoPairs + },{ + hand: [ '2♥','4♦','5♥','A♦','A♠' ], + expected: PokerRank.OnePair + },{ + hand: [ '3♥','4♥','10♥','3♦','A♠' ], + expected: PokerRank.OnePair + },{ + hand: [ '5♥','6♥','7♥','8♦','5♠' ], + expected: PokerRank.OnePair + },{ + hand: [ '3♥','4♥','5♥','7♦','8♥' ], + expected: PokerRank.HighCard + },{ + hand: [ 'A♥','K♥','Q♥','J♦','5♠' ], + expected: PokerRank.HighCard + },{ + hand: [ 'A♥','K♥','Q♥','2♦','3♠' ], + expected: PokerRank.HighCard + } ].forEach(data => { var actual = tasks.getPokerHandRank(data.hand); assert( @@ -249,86 +249,86 @@ describe('11-katas-2-tasks', function() { [ { figure: '+------------+\n'+ - '| |\n'+ - '| |\n'+ - '| |\n'+ - '+------+-----+\n'+ - '| | |\n'+ - '| | |\n'+ - '+------+-----+\n', + '| |\n'+ + '| |\n'+ + '| |\n'+ + '+------+-----+\n'+ + '| | |\n'+ + '| | |\n'+ + '+------+-----+\n', expected: [ - '+------------+\n'+ - '| |\n'+ - '| |\n'+ - '| |\n'+ - '+------------+\n', + '+------------+\n'+ + '| |\n'+ + '| |\n'+ + '| |\n'+ + '+------------+\n', - '+------+\n'+ - '| |\n'+ - '| |\n'+ - '+------+\n', + '+------+\n'+ + '| |\n'+ + '| |\n'+ + '+------+\n', - '+-----+\n'+ - '| |\n'+ - '| |\n'+ - '+-----+\n' + '+-----+\n'+ + '| |\n'+ + '| |\n'+ + '+-----+\n' ] },{ - figure: ' +-----+ \n'+ - ' | | \n'+ - '+--+-----+----+\n'+ - '| |\n'+ - '| |\n'+ - '+-------------+\n', - expected: [ - '+-----+\n'+ - '| |\n'+ - '+-----+\n', + figure: ' +-----+ \n'+ + ' | | \n'+ + '+--+-----+----+\n'+ + '| |\n'+ + '| |\n'+ + '+-------------+\n', + expected: [ + '+-----+\n'+ + '| |\n'+ + '+-----+\n', - '+-------------+\n'+ - '| |\n'+ - '| |\n'+ - '+-------------+\n' - ] - },{ - figure: ' +--+ \n'+ - ' | | \n'+ - '+--+--+--+\n'+ - '| | |\n'+ - '+--+--+--+\n'+ - ' | | \n'+ - ' +--+ \n', - expected: [ - '+--+\n'+ - '| |\n'+ - '+--+\n', + '+-------------+\n'+ + '| |\n'+ + '| |\n'+ + '+-------------+\n' + ] + },{ + figure: ' +--+ \n'+ + ' | | \n'+ + '+--+--+--+\n'+ + '| | |\n'+ + '+--+--+--+\n'+ + ' | | \n'+ + ' +--+ \n', + expected: [ + '+--+\n'+ + '| |\n'+ + '+--+\n', - '+--+\n'+ - '| |\n'+ - '+--+\n', + '+--+\n'+ + '| |\n'+ + '+--+\n', - '+--+\n'+ - '| |\n'+ - '+--+\n', + '+--+\n'+ + '| |\n'+ + '+--+\n', - '+-----+\n'+ - '| |\n'+ - '+-----+\n' - ] - },{ - figure: '++++\n'+ - '++++\n', - expected: [ - '++\n'+ - '++\n', + '+-----+\n'+ + '| |\n'+ + '+-----+\n' + ] + },{ + figure: '++++\n'+ + '++++\n', + expected: [ + '++\n'+ + '++\n', - '++\n'+ - '++\n', + '++\n'+ + '++\n', - '++\n'+ - '++\n', - ] - } + '++\n'+ + '++\n', + ] + } ].forEach(data => { var actual = Array.from(tasks.getFigureRectangles(data.figure)).sort(); var expected = data.expected.sort(); diff --git a/test/12-katas-3-tests.js b/test/12-katas-3-tests.js index 732e6e207..32e1318cc 100644 --- a/test/12-katas-3-tests.js +++ b/test/12-katas-3-tests.js @@ -7,7 +7,7 @@ it.optional = require('../extensions/it-optional'); describe('12-katas-3-tasks', function() { it.optional('findStringInSnakingPuzzle shoud return true if word occurrs in the specified puzzle', () => { - var puzzle = [ + var puzzle = [ 'ANGULAR', 'REDNCAE', 'RFIDTCL', @@ -41,20 +41,20 @@ describe('12-katas-3-tasks', function() { chars: 'a', expected: [ 'a' ] },{ - chars: 'ab', - expected: [ 'ab', 'ba' ] - },{ - chars: 'abc', - expected: [ 'abc', 'acb', 'bac', 'bca', 'cab', 'cba' ] - },{ - chars: 'abcd', - expected: [ - 'abcd', 'abdc', 'acbd', 'acdb', 'adbc', 'adcb', - 'bacd', 'badc', 'bcad', 'bcda', 'bdac', 'bdca', - 'cabd', 'cadb', 'cbad', 'cbda', 'cdab', 'cdba', - 'dabc', 'dacb', 'dbac', 'dbca', 'dcab', 'dcba' - ] - } + chars: 'ab', + expected: [ 'ab', 'ba' ] + },{ + chars: 'abc', + expected: [ 'abc', 'acb', 'bac', 'bca', 'cab', 'cba' ] + },{ + chars: 'abcd', + expected: [ + 'abcd', 'abdc', 'acbd', 'acdb', 'adbc', 'adcb', + 'bacd', 'badc', 'bcad', 'bcda', 'bdac', 'bdca', + 'cabd', 'cadb', 'cbad', 'cbda', 'cdab', 'cdba', + 'dabc', 'dacb', 'dbac', 'dbca', 'dcab', 'dcba' + ] + } ].forEach(data => { assert.deepEqual( Array.from(tasks.getPermutations(data.chars)).sort(), @@ -76,18 +76,18 @@ describe('12-katas-3-tasks', function() { quotes: [ 1, 2, 3, 4, 5, 6 ], expected: 15 },{ - quotes: [ 6, 5, 4, 3, 2, 1 ], - expected: 0 - },{ - quotes: [ 1, 6, 5, 10, 8, 7 ], - expected: 18 - },{ - quotes: [ 31, 312, 3, 35, 33, 3, 44, 123, 126, 2, 4, 1 ], - expected: 798 - },{ - quotes: [ 1, 20, 1, 30, 1, 40, 1, 50, 1, 40, 1, 30, 1, 20, 1 ], - expected: 343 - } + quotes: [ 6, 5, 4, 3, 2, 1 ], + expected: 0 + },{ + quotes: [ 1, 6, 5, 10, 8, 7 ], + expected: 18 + },{ + quotes: [ 31, 312, 3, 35, 33, 3, 44, 123, 126, 2, 4, 1 ], + expected: 798 + },{ + quotes: [ 1, 20, 1, 30, 1, 40, 1, 50, 1, 40, 1, 30, 1, 20, 1 ], + expected: 343 + } ].forEach(data => { var actual = tasks.getMostProfitFromStockQuotes(data.quotes); assert.equal(