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

Истомин Сергей #154

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
34 changes: 32 additions & 2 deletions roman-time.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
'use strict';

/**
* @param {number} num – число, в арабской системе
* @returns {String} – строку римских цифр
*/
function toRoman(num) {
var arab = [1, 4, 5, 9, 10, 40, 50];
var roman = ['I', 'IV', 'V', 'IX', 'X', 'XL', 'L'];
var j = arab.length - 1;
var res = '';
if (num === 0) {
res = 'N';
}
while (num > 0) {
if (num >= arab[j]) {
res += roman[j];
num -= arab[j];
} else {
j--;
}
}

return res;
}

/**
* @param {String} time – время в формате HH:MM (например, 09:05)
* @returns {String} – время римскими цифрами (IX:V)
*/
function romanTime(time) {
// Немного авторского кода и замечательной магии
var strs = time.split(':');
var numTime = [parseInt(strs[0], 10), parseInt(strs[1], 10)];
if (numTime[0].isNaN || numTime[1].isNaN || numTime[0] * numTime[1] < 0 ||
numTime[0] > 23 || numTime[1] > 59) {
throw new TypeError('Неверное время');
}
time = toRoman(numTime[0]) + ':' + toRoman(numTime[1]);

return time;
}

module.exports = romanTime;