forked from urfu-2016/javascript-task-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroman-time.js
More file actions
104 lines (66 loc) · 1.73 KB
/
roman-time.js
File metadata and controls
104 lines (66 loc) · 1.73 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';
var romanNumer = {
0: 'N',
1: 'I',
2: 'II',
3: 'III',
4: 'IV',
5: 'V',
6: 'VI',
7: 'VII',
8: 'VIII',
9: 'IX',
10: 'X',
20: 'XX',
30: 'XXX',
40: 'XL',
50: 'L'
};
// Выделяем десятки и единицы
function convertTime(time) {
var dozens = Math.floor(time / 10);
var unit = time % 10;
if (dozens === 0) {
return romanNumer[unit];
} else if (unit === 0) {
return romanNumer[dozens * 10];
}
return romanNumer[dozens * 10] + romanNumer[unit];
}
function checkNaN(hrs, min) {
var hours = parseInt(hrs, 10);
var minutes = parseInt(min, 10);
return isNaN(hours) || isNaN(minutes);
}
function checkRegExp(time) {
var regExp = /^([0-1]\d?[0-9]|2[0-3])(:[0-5][0-9])$/;
return regExp.test(time);
}
function checkType(time) {
return typeof time === 'string';
}
function checkNullUndf(time) {
if (time === null || time === undefined) {
return true;
}
return false;
}
/**
* @param {String} time – время в формате HH:MM (например, 09:05)
* @returns {String} – время римскими цифрами (IX:V)
*/
function romanTime(time) {
var splitDate = time.split(':');
var getHour = parseInt(splitDate[0], 10);
var getMin = parseInt(splitDate[1], 10);
if (!checkType(time) || checkNullUndf(time) || checkNaN(getHour, getMin)) {
throw new TypeError('Incorrect time format!');
} else if (!checkRegExp(time)) {
throw new TypeError('Incorrect time format!');
}
var HH = convertTime(getHour);
var MM = convertTime(getMin);
time = HH + ':' + MM;
return time;
}
module.exports = romanTime;