praca-test-tasks/4.nodejs_js/index.js
2023-03-12 13:58:18 +03:00

41 lines
1.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function isInt (value) { // Функция проверки целочисленного типа
if (typeof(value) == 'number') {
return parseInt(value) == value;
}
else {
return false;
}
}
function isArray (value, typeCheck=null) { // Функция проверки типа массива
if (!Array.isArray(value)) {
return false;
}
else if (typeCheck == null) {
return true
}
else {
for (let i in value) {
if (!typeCheck(value[i])) {
return false;
}
}
return true;
}
}
function getTwoSummands (array, findValue) {
if (isInt(findValue) && isArray(array, isInt)) { // Вы же ещё помните, что javascript - язык с динамической, а не статической типизацией данных?
let calculatedVal;
for (let i in array) {
calculatedVal = findValue - array[i];
if (array.indexOf(calculatedVal) != -1 && array.indexOf(calculatedVal) != i) { // Проверим есть ли значение в массиве и имеет ли данное значение рассматриваемый индекс.
return [array[i], calculatedVal];
}
}
return [];
}
else {
throw new Error("Wrong arguments: <Array(int)>, <int>");
}
}