-
Notifications
You must be signed in to change notification settings - Fork 32
Козельских_ДЗ первой недели #37
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. добавить проверку входного массива на корректность |
||
if (array[i] === 0) { | ||
array.splice(i, 1) | ||
i-- | ||
} else { | ||
array.splice(i, 0, array[i]) | ||
i++ | ||
} | ||
} | ||
return array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. calculateDoubleArray([1, 2, 3, 4, 5]) - вернёт тот же результат что и [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]. |
||
} | ||
|
||
module.exports.calculateDoubleArray = calculateDoubleArray; | ||
|
||
module.exports.calculateDoubleArray = calculateDoubleArray; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,19 @@ | |
*/ | ||
|
||
function factorial(n) { | ||
if (n > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. нет проверки, что входной аргумент число, и что оно целое |
||
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,18 @@ | |
* @param {string[]} nameArray - массив имён. | ||
* @return {number} Количество уникальных имён | ||
* */ | ||
|
||
function countUniqueName(nameArray) { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
var lowerArray = []; | ||
for (i = 0; i < nameArray.length; i++) { | ||
let name = nameArray[i].toString().toLowerCase() | ||
lowerArray.push(name) | ||
} | ||
|
||
const unique = new Set(lowerArray); | ||
|
||
return unique.size | ||
} | ||
|
||
module.exports.countUniqueName = countUniqueName; | ||
module.exports.countUniqueName = countUniqueName; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,32 @@ | |
* Предусмотреть крайние случаи, для входных данных и при необходимости | ||
* выкидывать исключение. | ||
* */ | ||
function kaprekarConstant(number){ | ||
|
||
function kaprekarConstant(number) { | ||
let countDigits = number.toString().length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kaprekarConstant(2223) - зависает. Добавить обработку входных данных |
||
//Шаг первый: проверка на коиличество цифр. Должно быть 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-х цифр.') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
https://www.dcode.fr/kaprekar-algorithm - я тестил тут |
||
} | ||
} | ||
|
||
module.exports.kaprekarConstant = kaprekarConstant; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 целочисленных значения
добавить валидацию входных аргументов