forked from urfu-2016/javascript-task-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman-time.js
64 lines (54 loc) · 1.36 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
56
57
58
59
60
61
62
63
64
'use strict';
/**
* @param {String} time – время в формате HH:MM (например, 09:05)
* @returns {String} – время римским цифрами (IX:V)
*/
function romanTime(time) {
var t = checkTime(time);
var hh = t[0];
var mm = t[1];
var hours = convert(hh);
var min = convert(mm);
if (isNaN(hours) === false) {
hours = "N";
}
if (isNaN(min) === false) {
min = "N";
}
time = hours + ':' + min;
return time;
}
function checkTime(time) {
if (time.length > 5) {
throw new TypeError('Неверный ввод!');
}
var t = time.split(':');
t[0] = Number(t[0]);
t[1] = Number(t[1]);
checkHours(t[0]);
checkMinutes(t[1]);
return t;
}
function checkHours(h) {
if (h > 23 || h < 0 || isNaN(h)) {
throw new TypeError('Неверный ввод!');
}
}
function checkMinutes(m) {
if (m > 59 || m < 0 || isNaN(m)) {
throw new TypeError('Неверный ввод!');
}
}
function convert(num) {
var lookupSymb = ['L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
var lookup = [50, 40, 10, 9, 5, 4, 1];
var roman = '';
for (var i = 0; i < lookup.length; i++) {
while (num >= lookup[i]) {
roman += lookupSymb[i];
num -= lookup[i];
}
}
return roman;
}
module.exports = romanTime;