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

Заморский Глеб #175

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
44 changes: 42 additions & 2 deletions roman-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,48 @@
* @returns {String} – время римскими цифрами (IX:V)
*/
function romanTime(time) {
// Немного авторского кода и замечательной магии
return time;

if (typeof(time) !== 'string') {
throw new TypeError('Введены неверные данные');
}

function arabToRoman(number) {
let arab = [1, 4, 5, 9, 10, 40, 50];
let roman = ['I', 'IV', 'V', 'IX', 'X', 'XL', 'L'];
let result = '';

for (let i = 6; number > 0;) {
if (number >= arab[i]) {
result += roman[i];
number -= arab[i];
i = 6;
}
i--;
}

return result;
}

let strArr = time.split(':').slice(0, 2);
if (strArr.length > 2) {
throw new TypeError('Введены неверные данные');
}
let parseArr = strArr.map(function (item) {
if ((strArr[0] >= 0 && strArr[0] <= 23) && (strArr[1] >= 0 && strArr[1] <= 59)) {
return parseInt(item);
}
throw new TypeError('Введены неверные данные');
});

let result = parseArr.map(function (item) {
if (item === 0) {
return 'N';
}

return arabToRoman(item);
});

return result.join(':');
}

module.exports = romanTime;