forked from urfu-2016/javascript-task-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman-time.js
55 lines (44 loc) · 1.32 KB
/
roman-time.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
var roman = ['L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
var arab = [50, 40, 10, 9, 5, 4, 1];
function arabToRoman(num) {
if (num === 0) {
return 'N';
}
var result = '';
for (var i = 0; i < arab.length; i++) {
while (num % arab[i] < num) {
result += roman[i];
num -= arab[i];
}
}
return result;
}
function isString(time) {
var isNullOrUndefined = time === null || time === undefined;
return typeof time === 'string' || time instanceof String || !isNullOrUndefined;
}
function isTimeInBounds(hours, mins) {
return mins >= 0 && mins < 60 && hours >= 0 && hours < 24;
}
/**
* @param {String} time – время в формате HH:MM (например, 09:05)
* @returns {String} – время римскими цифрами (IX:V)
*/
function romanTime(time) {
var error = new TypeError('Неверное время');
if (!isString(time)) {
throw error;
}
time = time.match(/^(\d{2}):(\d{2})$/);
if (time === null || time[1] === undefined || time[2] === undefined) {
throw error;
}
var mins = Number(time[2]);
var hours = Number(time[1]);
if (!isTimeInBounds(hours, mins)) {
throw error;
}
return arabToRoman(hours) + ':' + arabToRoman(mins);
}
module.exports = romanTime;