forked from urfu-2017/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
61 lines (54 loc) · 1.5 KB
/
roman-time.js
File metadata and controls
61 lines (54 loc) · 1.5 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
'use strict';
/**
* @param {String} time – время в формате HH:MM (например, 09:05)
* @returns {String} – время римскими цифрами (IX:V)
*/
function romanTime(time) {
var template = new RegExp(/^\d\d:\d\d$/);
if (template.test(time) === false) {
throw new TypeError();
}
var times = time.split(':');
if (Number(times[0]) > 23 || Number(times[1]) > 59) {
throw new TypeError();
}
var hours = (times[0] === '00') ? 'N' : returnRomanTime(times[0]);
var minutes = (times[1] === '00') ? 'N' : returnRomanTime(times[1]);
return hours + ':' + minutes;
}
function returnRomanTime(number) {
var firstPart = number.charAt(0);
var secondPart = number.charAt(1);
if (firstPart === '1') {
return 'X' + getSecondPart(secondPart);
}
if (firstPart === '2') {
return 'XX' + getSecondPart(secondPart);
}
if (firstPart === '3') {
return 'XXX' + getSecondPart(secondPart);
}
if (firstPart === '4') {
return 'XL' + getSecondPart(secondPart);
}
if (firstPart === '5') {
return 'L' + getSecondPart(secondPart);
}
return getSecondPart(secondPart);
}
function getSecondPart(number) {
var dictionary = {
'0': '',
'1': 'I',
'2': 'II',
'3': 'III',
'4': 'IV',
'5': 'V',
'6': 'VI',
'7': 'VII',
'8': 'VIII',
'9': 'IX'
};
return dictionary[number];
}
module.exports = romanTime;