From df4c9dc9b044a22e1309c790e9b515a6f4208e3e Mon Sep 17 00:00:00 2001 From: AKozelskikh Date: Wed, 6 Oct 2021 09:53:56 +0500 Subject: [PATCH 1/3] =?UTF-8?q?=D0=94=D0=97=20=D0=BF=D0=B5=D1=80=D0=B2?= =?UTF-8?q?=D0=BE=D0=B9=20=D0=BD=D0=B5=D0=B4=D0=B5=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- topic-1/task-1/index.js | 8 +++++++- topic-1/task-2/index.js | 22 ++++++++++++++++++++-- topic-1/task-3/index.js | 13 +++++++++++++ topic-1/task-4/index.js | 17 ++++++++++++++++- topic-1/task-5/index.js | 30 +++++++++++++++++++++++++++++- 5 files changed, 85 insertions(+), 5 deletions(-) diff --git a/topic-1/task-1/index.js b/topic-1/task-1/index.js index d76557c..1e8ee69 100644 --- a/topic-1/task-1/index.js +++ b/topic-1/task-1/index.js @@ -10,7 +10,13 @@ * @returns */ - function isTriangle(a, b, c) { +isTriangle(7, 2, 2) +function isTriangle(a, b, c) { + if (a + b > c && a + c > b && b + c > a) { + return true; + } else { + return false; + } } module.exports.isTriangle = isTriangle; \ No newline at end of file diff --git a/topic-1/task-2/index.js b/topic-1/task-2/index.js index 093141c..5b424bf 100644 --- a/topic-1/task-2/index.js +++ b/topic-1/task-2/index.js @@ -11,7 +11,25 @@ * @param {*} array массив * @returns удвоенный массив */ - function calculateDoubleArray(array) { + +let array = [1, 2, 3, 4, 5, 0, 0, 0, 0, 0] + +calculateDoubleArray(array) + +function calculateDoubleArray(array) { + for (let i = 0; i < array.length; i++) { + if (array[i] === 0) { + array.splice(i, 1) + i-- + } else { + array.splice(i, 0, array[i]) + i++ + } + } + return array } -module.exports.calculateDoubleArray = calculateDoubleArray; \ No newline at end of file + +module.exports.calculateDoubleArray = calculateDoubleArray; + + diff --git a/topic-1/task-3/index.js b/topic-1/task-3/index.js index 7e12c79..b0c8948 100644 --- a/topic-1/task-3/index.js +++ b/topic-1/task-3/index.js @@ -10,6 +10,19 @@ */ function factorial(n) { + if (n > 0) { + let temp = 1 + for (let i = n; i > 0; i--) { + temp = temp * i + } + return temp + } + if (n == 0) { + return 1 + } else { + throw new Error("Число меньше 0") + } } + module.exports.factorial = factorial; \ No newline at end of file diff --git a/topic-1/task-4/index.js b/topic-1/task-4/index.js index da33ee2..b1b07fe 100644 --- a/topic-1/task-4/index.js +++ b/topic-1/task-4/index.js @@ -6,7 +6,22 @@ * @param {string[]} nameArray - массив имён. * @return {number} Количество уникальных имён * */ + function countUniqueName(nameArray) { + + var lowerArray = []; + for (i = 0; i < nameArray.length - 1; i++) { + let name = nameArray[i].toString().toLowerCase().replace(/[; .,!?:–]/, "") + if (nameArray[i] != "") { + lowerArray.push(name) + } + } + + const unique = new Set(lowerArray); + + return unique.size } -module.exports.countUniqueName = countUniqueName; \ No newline at end of file +countUniqueName(arrayOfName) + +module.exports.countUniqueName = countUniqueName; diff --git a/topic-1/task-5/index.js b/topic-1/task-5/index.js index d0760a9..f99cc9c 100644 --- a/topic-1/task-5/index.js +++ b/topic-1/task-5/index.js @@ -4,7 +4,35 @@ * Предусмотреть крайние случаи, для входных данных и при необходимости * выкидывать исключение. * */ -function kaprekarConstant(number){ + +function kaprekarConstant(number) { + let countDigits = number.toString().length; + //Шаг первый: проверка на коиличество цифр. Должно быть 4. + if (countDigits === 4) { + var digitToArray = number.toString().split(""); + var arrayToCompareDigit = new Set(digitToArray) + //Шаг второй: проверка на коиличество повторений цифр. Должна хотя бы одна не повториться. + if (arrayToCompareDigit.size > 1) { + var result = number + //Проверяем, выполнено ли условие + while (result != 6174) { + result = result.toString().split(""); + //Шаг третий: создания чисел из исходного с отсортированными цифрами от большего к меньшему и от меньшего к большему + let numberFromBigToSmall = result.sort((a, b) => b - a).join(''); + let numberFromSmallToBIG = result.sort((a, b) => a - b).join(''); + //Шаг четвертый: вычетаем из большего меньшее + var result = numberFromBigToSmall - numberFromSmallToBIG + } + return result + } else { + throw new Error('Число не должно состоять из одинаковых цифр.') + } + } else { + throw new Error('Число не из 4-х цифр.') + } } +//для отладки +kaprekarConstant(1234) + module.exports.kaprekarConstant = kaprekarConstant; \ No newline at end of file From b0c04b12a3b4412e6c867f2a65ae0755e5f501bc Mon Sep 17 00:00:00 2001 From: AKozelskikh Date: Wed, 6 Oct 2021 11:32:31 +0500 Subject: [PATCH 2/3] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=82=D0=B0=D1=81=D0=BA-4,=20=D1=82?= =?UTF-8?q?=D0=B0=D1=81=D0=BA-5=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20=D0=B0?= =?UTF-8?q?=D0=B2=D1=82=D0=BE=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- topic-1/task-4/index.js | 2 -- topic-1/task-5/index.js | 3 --- 2 files changed, 5 deletions(-) diff --git a/topic-1/task-4/index.js b/topic-1/task-4/index.js index b1b07fe..80c79cc 100644 --- a/topic-1/task-4/index.js +++ b/topic-1/task-4/index.js @@ -22,6 +22,4 @@ function countUniqueName(nameArray) { return unique.size } -countUniqueName(arrayOfName) - module.exports.countUniqueName = countUniqueName; diff --git a/topic-1/task-5/index.js b/topic-1/task-5/index.js index f99cc9c..bcc6a39 100644 --- a/topic-1/task-5/index.js +++ b/topic-1/task-5/index.js @@ -32,7 +32,4 @@ function kaprekarConstant(number) { } } -//для отладки -kaprekarConstant(1234) - module.exports.kaprekarConstant = kaprekarConstant; \ No newline at end of file From 982a1a8b1cef1448575f31b5b7a1a46110d2059f Mon Sep 17 00:00:00 2001 From: AKozelskikh Date: Wed, 6 Oct 2021 11:49:38 +0500 Subject: [PATCH 3/3] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=A2=D0=B0=D1=81=D0=BA-4=20=D0=BF?= =?UTF-8?q?=D0=BE=D1=81=D0=BB=D0=B5=20=D0=B0=D0=B2=D1=82=D0=BE=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- topic-1/task-4/index.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/topic-1/task-4/index.js b/topic-1/task-4/index.js index 80c79cc..001cf14 100644 --- a/topic-1/task-4/index.js +++ b/topic-1/task-4/index.js @@ -10,11 +10,9 @@ function countUniqueName(nameArray) { var lowerArray = []; - for (i = 0; i < nameArray.length - 1; i++) { - let name = nameArray[i].toString().toLowerCase().replace(/[; .,!?:–]/, "") - if (nameArray[i] != "") { - lowerArray.push(name) - } + for (i = 0; i < nameArray.length; i++) { + let name = nameArray[i].toString().toLowerCase() + lowerArray.push(name) } const unique = new Set(lowerArray);