forked from urfu-2016/javascript-task-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman-time.js
59 lines (46 loc) · 1.56 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
'use strict';
function checkLength(hours, minutes) {
var checkMore = hours > 23 || minutes > 59;
var checkMin = hours < 0 || minutes < 0;
return checkMin || checkMore;
}
function checkIsNaNOrNull(hours, minutes) {
var checkNaN = (isNaN(hours) || isNaN(minutes));
var checkNull = (hours === null || minutes === null);
return checkNaN || checkNull;
}
function check(hours, minutes) {
if (checkLength(hours, minutes) || checkIsNaNOrNull(hours, minutes)) {
throw new TypeError('Incorrect time');
}
return true;
}
function timeTranslator(time, arrMinutes, arrHours) {
var first = arrHours[parseInt(time / 10)];
var second = arrMinutes[parseInt(time % 10)];
if (first === 'N' && second !== 'N') {
return second;
}
if (time === 0) {
return 'N';
}
var newTime = first + second;
return newTime;
}
function romanTime(time) {
// Немного авторского кода и замечательной магии
var arrTime = time.split(':');
var hours = Number(arrTime[0]);
var minutes = Number(arrTime[1]);
if (time.length !== 5) {
throw new TypeError('Incorrect time');
}
check(hours, minutes);
var arrMinutes = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'];
var arrHours = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX'];
var romanHours = timeTranslator(hours, arrMinutes, arrHours);
var romanMinutes = timeTranslator(minutes, arrMinutes, arrHours);
time = romanHours + ':' + romanMinutes;
return time;
}
module.exports = romanTime;