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
44 lines (39 loc) · 1.24 KB
/
roman-time.js
File metadata and controls
44 lines (39 loc) · 1.24 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
'use strict';
/*
* @param {String} time – время в формате HH:MM (например, 09:05)
* @returns {String} – время римскими цифрами (IX:V)
*/
function translate(arabNumber, firstNumeral) {
var result = '';
const romanDict = {
0: '', 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'
};
var secondNumeral = 0;
if (firstNumeral > 9) {
secondNumeral = firstNumeral % 10;
}
if (firstNumeral === 0) {
result = 'N';
} else {
result = romanDict[firstNumeral - secondNumeral] + romanDict[secondNumeral];
}
return result;
}
function romanTime(time) {
var reg = new RegExp('([0-2])?[0-9]:([0-5])?[0-9]');
var rTime = reg.exec(time)[0];
var romanDate = rTime.split(':');
var roman = [];
var hh = parseInt(romanDate[0], 10);
var mm = parseInt(romanDate[1], 10);
if (time.length === rTime.length && hh >= 0 && hh <= 23) {
roman[0] = translate(romanDate[0], hh);
roman[1] = translate(romanDate[1], mm);
} else {
throw new TypeError();
}
time = roman.join(':');
return time;
}
module.exports = romanTime;