Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Малых Денис #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 148 additions & 6 deletions warmup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
* @returns {Number} Сумма аргументов
*/
function abProblem(a, b) {
// Ваше решение
if (typeof(a) !== 'number') {
throw new TypeError();
}
if (typeof(b) !== 'number') {
throw new TypeError();
}

return a + b;
}

/**
Expand All @@ -19,6 +26,14 @@ function abProblem(a, b) {
* @returns {Number} Век, полученный из года
*/
function centuryByYearProblem(year) {
if (typeof(year) !== 'number') {
throw new TypeError();
}
if (year < 0) {
throw new RangeError();
}

return Math.ceil(year / 100);
// Ваше решение
}

Expand All @@ -30,7 +45,14 @@ function centuryByYearProblem(year) {
* @returns {String} Цвет в формате RGB, например, '(255, 255, 255)'
*/
function colorsProblem(hexColor) {
// Ваше решение
if (typeof(hexColor) !== 'string') {
throw new TypeError();
}
let red = parseInt(hexColor.substring(1, 3), 16);
let green = parseInt(hexColor.substring(3, 5), 16);
let blue = parseInt(hexColor.substring(5, 7), 16);

return `(${red}, ${green}, ${blue})`;
}

/**
Expand All @@ -41,7 +63,19 @@ function colorsProblem(hexColor) {
* @returns {Number} Число Фибоначчи, находящееся на n-ой позиции
*/
function fibonacciProblem(n) {
// Ваше решение
if (typeof(n) !== 'number') {
throw new TypeError();
}
if (n <= 0) {
throw new RangeError();
}
function fib(x) {
return Math.round((Math.pow((1 + Math.sqrt(5)) / 2, x) -
Math.pow((1 - Math.sqrt(5)) / 2, x)) /
Math.sqrt(5));
}

return fib(n);
}

/**
Expand All @@ -51,7 +85,39 @@ function fibonacciProblem(n) {
* @returns {(Any[])[]} Транспонированная матрица размера NxM
*/
function matrixProblem(matrix) {
// Ваше решение
if (!Array.isArray(matrix) || matrix.length === 0) {
throw new TypeError();
}

let m = matrix.length;
let n = matrix[0].length;

for (let i = 0; i < m; ++i) {
if (!Array.isArray(matrix[i]) || matrix[i].length !== n) {
throw new TypeError();
}
}

return logic(matrix);

function logic() {
if (n === 0) {
throw new TypeError();
}

let arr = new Array(n);

for (let i = 0; i < n; ++i) {
arr[i] = new Array(m);
}
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
arr[j][i] = matrix[i][j];
}
}

return arr;
}
}

/**
Expand All @@ -63,7 +129,14 @@ function matrixProblem(matrix) {
* @returns {String} Число n в системе счисления targetNs
*/
function numberSystemProblem(n, targetNs) {
// Ваше решение
if (typeof(n) !== 'number' || typeof(targetNs) !== 'number') {
throw new TypeError();
}
if (targetNs > 36 || targetNs < 2) {
throw new RangeError();
}

return n.toString(targetNs);
}

/**
Expand All @@ -72,7 +145,12 @@ function numberSystemProblem(n, targetNs) {
* @returns {Boolean} Если соответствует формату, то true, а иначе false
*/
function phoneProblem(phoneNumber) {
// Ваше решение
if (typeof(phoneNumber) !== 'string') {
throw new TypeError();
}
const regexp = new RegExp(/^8-800-[0-9]{3}-[0-9]{2}-[0-9]{2}$/g);

return regexp.test(phoneNumber);
}

/**
Expand All @@ -82,6 +160,22 @@ function phoneProblem(phoneNumber) {
* @returns {Number} Количество улыбающихся смайликов в строке
*/
function smilesProblem(text) {
if (typeof(text) !== 'string') {
throw new TypeError();
}
let res = 0;
for (let i = 0; i < text.length; i++) {
if ((text[i] === '(') && (text[i + 1] === '-') && (text[i + 2] === ':')) {
i += 2;
res++;
}
if (text[i] === ':' && text[i + 1] === '-' && text[i + 2] === ')') {
i += 2;
res++;
}
}

return res;
// Ваше решение
}

Expand All @@ -92,6 +186,54 @@ function smilesProblem(text) {
* @returns {'x' | 'o' | 'draw'} Результат игры
*/
function ticTacToeProblem(field) {
if (diagonalCheck(field) !== 'n') {
return diagonalCheck(field);
} else if (verticalCheck(field) !== 'n') {
return verticalCheck(field);
} else if (horizontalCheck(field) !== 'n') {
return horizontalCheck(field);
}

return 'draw';
}

function diagonalCheck(field) {
if (field[0][0] === field[1][1] && field[1][1] === field[2][2]) {
return field[1][1];
} else if (field[2][0] === field[1][1] && field[1][1] === field[0][2]) {
return field[1][1];
}

return 'n';
}

function verticalCheck(field) {
field = matrixProblem(field);
for (let i = 0; i < 3; i++) {
if (checkLine(field[i]) === 'o' || checkLine(field[i]) === 'x') {
return checkLine(field[i]);
}
}

return 'n';
}

function horizontalCheck(field) {
for (let i = 0; i < 3; i++) {
if (checkLine(field[i]) === 'o' || checkLine(field[i]) === 'x') {
return checkLine(field[i]);
}
}

return 'n';
}

function checkLine(line) {
if (line[0] === line[1] && line[1] === line[2]) {
return line[0];
}

return 'n';
// Ваше решение
}

Expand Down