diff --git a/.gitignore b/.gitignore index 552f22184..6896da6d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ *.log +.idea diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 000000000..6a3d3c7a7 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,21 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 000000000..79ee123c2 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 000000000..c6cc8c819 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/javascript-task-1.iml b/.idea/javascript-task-1.iml new file mode 100644 index 000000000..24643cc37 --- /dev/null +++ b/.idea/javascript-task-1.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jsLibraryMappings.xml b/.idea/jsLibraryMappings.xml new file mode 100644 index 000000000..d23208fbb --- /dev/null +++ b/.idea/jsLibraryMappings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..28a804d89 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000..e4430f417 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..94a25f7f4 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 000000000..878dd7e6f --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,474 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + true + true + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + $PROJECT_DIR$ + true + bdd + + TEST + $PROJECT_DIR$/warmup.spec.js + + + + + + + + project + + $PROJECT_DIR$ + true + bdd + + TEST + $PROJECT_DIR$/warmup.spec.js + + + + + + + + project + + $PROJECT_DIR$ + true + bdd + + TEST + $PROJECT_DIR$/warmup.spec.js + + + + + + + + project + + $PROJECT_DIR$ + true + bdd + + SUITE + $PROJECT_DIR$/warmup.spec.js + + + + + + + project + + $PROJECT_DIR$ + true + bdd + + TEST + $PROJECT_DIR$/warmup.spec.js + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1539191367618 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/warmup.js b/warmup.js index eefda9891..d7e659086 100644 --- a/warmup.js +++ b/warmup.js @@ -8,7 +8,15 @@ * @returns {Number} Сумма аргументов */ function abProblem(a, b) { - // Ваше решение + if (Math.trunc(a) !== a || Math.trunc(b) !== b) { + throw new TypeError(); + } + + if (typeof a === 'number' && typeof b === 'number') { + return a + b; + } + + throw new TypeError(); } /** @@ -19,7 +27,25 @@ function abProblem(a, b) { * @returns {Number} Век, полученный из года */ function centuryByYearProblem(year) { - // Ваше решение + if (Math.trunc(year) !== year) { + throw new TypeError(); + } + + if (typeof year !== 'number') { + throw new TypeError(); + } + + if (year < 0) { + throw new RangeError(); + } + + if (year % 100 !== 0) { + return Math.trunc(year / 100) + 1; + } + + if (year % 100 === 0) { + return year / 100; + } } /** @@ -30,7 +56,23 @@ function centuryByYearProblem(year) { * @returns {String} Цвет в формате RGB, например, '(255, 255, 255)' */ function colorsProblem(hexColor) { - // Ваше решение + if (typeof hexColor !== 'string') { + throw new TypeError(); + } + + if (hexColor.length !== 7) { + throw new RangeError(); + } + + if (!/#[a-f|A-F|0-9]{6}/.test(hexColor)) { + throw new RangeError(); + } + + const red = parseInt(hexColor.substr(1, 2), 16); + const green = parseInt(hexColor.substr(3, 2), 16); + const blue = parseInt(hexColor.substr(5, 2), 16); + + return `(${red}, ${green}, ${blue})`; } /** @@ -41,7 +83,25 @@ function colorsProblem(hexColor) { * @returns {Number} Число Фибоначчи, находящееся на n-ой позиции */ function fibonacciProblem(n) { - // Ваше решение + if (typeof n !== 'number') { + throw new TypeError(); + } + + if (n < 1 || Math.trunc(n) !== n) { + throw new RangeError(); + } + + let n1 = 0; + let n2 = 1; + + for (let i = 1; i <= n - 1; i++) { + let n11 = n1; + n1 = n2; + n2 = n11 + n2; + } + + return n2; + } /** @@ -51,7 +111,29 @@ function fibonacciProblem(n) { * @returns {(Any[])[]} Транспонированная матрица размера NxM */ function matrixProblem(matrix) { - // Ваше решение + + if (matrix.length === 0) { + return []; + } + + for (let i = 0; i < matrix.length; i++) { + if (!Array.isArray(matrix[i])) { + throw new TypeError(); + } + } + + let matrixResult = []; + + for (let x = 0; x < matrix[0].length; x++) { + let row = []; + for (let y = 0; y < matrix.length; y++) { + row.push(matrix[y][x]); + } + + matrixResult.push(row); + } + + return matrixResult; } /** @@ -62,8 +144,67 @@ function matrixProblem(matrix) { * @throws {RangeError} Когда система счисления выходит за пределы значений [2, 36] * @returns {String} Число n в системе счисления targetNs */ +const abc = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', + 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', + 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; + +// eslint-disable-next-line function numberSystemProblem(n, targetNs) { - // Ваше решение + if (typeof n !== 'number') { + throw new TypeError(); + } + + if (typeof targetNs !== 'number') { + throw new TypeError(); + } + + if (targetNs < 2 || targetNs > 36) { + throw new RangeError(); + } + + if (n < 0) { + n = -n; + } + + let [int, d] = n.toString().split('.'); + let result = toTargetNs(int, targetNs); + + if (d) { + result += '.'; + d = n - parseInt(int); + while (d !== 0) { + d = d * 2; + const [i] = d.toString().split('.'); + result += abc[i.toString()]; + d = d - i; + } + } + + if (n < 0) { + result = '-' + result; + } + + return result.toLowerCase(); +} + +function toTargetNs(n, targetNs) { + let result = ''; + + while (n >= targetNs) { + const ost = n % targetNs; + result = result + abc[ost]; + n = (n - ost) / targetNs; + } + + if (n !== 0 || result.length === 0) { + result += abc[n]; + } + + result = result.split('') + .reverse() + .join(''); + + return result.toLocaleLowerCase(); } /** @@ -72,7 +213,11 @@ function numberSystemProblem(n, targetNs) { * @returns {Boolean} Если соответствует формату, то true, а иначе false */ function phoneProblem(phoneNumber) { - // Ваше решение + if (typeof phoneNumber !== 'string') { + throw new TypeError(); + } + + return /^8-800-\d\d\d-\d\d-\d\d$/.test(phoneNumber); } /** @@ -82,7 +227,19 @@ function phoneProblem(phoneNumber) { * @returns {Number} Количество улыбающихся смайликов в строке */ function smilesProblem(text) { - // Ваше решение + const matchResult = text.match(/(:-\))/g); + const matchResult2 = text.match(/(\(-:)/g); + let result = 0; + + if (matchResult) { + result += matchResult.length; + } + + if (matchResult2) { + result += matchResult2.length; + } + + return result; } /** @@ -91,8 +248,56 @@ function smilesProblem(text) { * @param {(('x' | 'o')[])[]} field Игровое поле 3x3 завершённой игры * @returns {'x' | 'o' | 'draw'} Результат игры */ +// eslint-disable-next-line function ticTacToeProblem(field) { - // Ваше решение + + for (let i = 1; i <= 2; i++) { + if (field[0].join('') === 'xxx') { + return 'x'; + } + if (field[0].join('') === 'ooo') { + return 'o'; + } + if (field[1].join('') === 'xxx') { + return 'x'; + } + + if (field[1].join('') === 'ooo') { + return 'o'; + } + + if (field[2].join('') === 'xxx') { + return 'x'; + } + + if (field[2].join('') === 'ooo') { + return 'o'; + } + + field = matrixProblem(field); + } + + const diag1 = field[0][0] + field[1][1] + field[2][2]; + const diag2 = field[0][2] + field[1][1] + field[2][0]; + + if (diag1 === 'xxx') { + return 'x'; + } + + if (diag1 === 'ooo') { + return 'o'; + } + + if (diag2 === 'xxx') { + return 'x'; + } + + if (diag2 === 'ooo') { + return 'o'; + } + + return 'draw'; + } module.exports = { diff --git a/warmup.spec.js b/warmup.spec.js index 1835c49be..33be2fabd 100644 --- a/warmup.spec.js +++ b/warmup.spec.js @@ -25,18 +25,46 @@ describe('Century by year problem', () => { it('Должна вернуть `21`', () => { assert.strictEqual(centuryByYearProblem(2018), 21); }); + + it('Должен вернуть 1', () => { + assert.strictEqual(centuryByYearProblem(1), 1); + }); + + it('Должен вернуть 2', () => { + assert.strictEqual(centuryByYearProblem(200), 2); + }); + it('Должна вернуть `10`', () => { + assert.strictEqual(centuryByYearProblem(1000), 10); + }); + }); describe('Colors problem', function () { it('Должна вернуть (255, 255, 255)', function () { assert.strictEqual(colorsProblem('#FFFFFF'), '(255, 255, 255)'); }); + + it('Ошибка', function () { + try { + colorsProblem('#ABCDEZ'); + } catch (e) { + assert.ok('s'); + } + }); }); describe('Fibonacci problem', () => { it('Для n=1 должна вернуть `1`', () => { assert.strictEqual(fibonacciProblem(1), 1); }); + + it('Для n=2 должна вернуть `1`', () => { + assert.strictEqual(fibonacciProblem(2), 1); + }); + + it('Для n=3 должна вернуть `2`', () => { + assert.strictEqual(fibonacciProblem(3), 2); + }); }); describe('Matrix problem', () => { @@ -52,12 +80,49 @@ describe('Matrix problem', () => { [3, 6, 9] ]); }); + + it('Должна транспонировать квадратную матрицу 3x3', () => { + assert.deepStrictEqual( + matrixProblem([ + [1, 2, 3, 4], + [4, 5, 6, 7], + [7, 8, 9, 10] + ]), [ + [1, 4, 7], + [2, 5, 8], + [3, 6, 9], + [4, 7, 10] + ]); + }); + + it('Должна транспонировать квадратную матрицу 3x3', () => { + assert.deepStrictEqual( + matrixProblem([ + [1, 2, 3, 4, 5], + [4, 5, 6, 7, 8], + [7, 8, 9, 10, 11] + ]), [ + [1, 4, 7], + [2, 5, 8], + [3, 6, 9], + [4, 7, 10], + [5, 8, 11] + ]); + }); }); describe('Number System Problem', () => { it('Должна вернуть "101"', () => { assert.strictEqual(numberSystemProblem(5, 2), '101'); }); + + it('Должна вернуть "0"', () => { + assert.strictEqual(numberSystemProblem(0, 2), '0'); + }); + + it('Должна вернуть "5"', () => { + assert.strictEqual(numberSystemProblem(5, 16), '5'); + }); }); describe('Phone problem', () => { @@ -70,6 +135,14 @@ describe('Smiles problem', () => { it('Должна вернуть `1`', () => { assert.strictEqual(smilesProblem(':-)'), 1); }); + + it('Должна вернуть `3`', () => { + assert.strictEqual(smilesProblem(':-))) lskdjflskdfj :-)fsldkjflsd :-)'), 3); + }); + + it('Должна вернуть `0`', () => { + assert.strictEqual(smilesProblem('ljdslkfjsdlf sdfj lsdfj'), 0); + }); }); describe('Tic-tac-toe problem', () => { @@ -80,4 +153,28 @@ describe('Tic-tac-toe problem', () => { ['o', 'x', 'o'] ]), 'x'); }); + + it('Должна вернуть "o"', () => { + assert.strictEqual(ticTacToeProblem([ + ['o', 'x', 'x'], + ['o', 'o', 'x'], + ['o', 'x', 'o'] + ]), 'o'); + }); + + it('Должна вернуть "draw"', () => { + assert.strictEqual(ticTacToeProblem([ + ['x', 'o', 'x'], + ['o', 'o', 'x'], + ['o', 'x', 'o'] + ]), 'draw'); + }); + + it('Должна вернуть "x"', () => { + assert.strictEqual(ticTacToeProblem([ + ['o', 'o', 'x'], + ['o', 'x', 'x'], + ['x', 'o', 'o'] + ]), 'x'); + }); });