forked from urfu-2017/javascript-task-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman-time.js
37 lines (31 loc) · 1.11 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
'use strict';
var associations = { 1: 'I', 4: 'IV', 5: 'V', 9: 'IX', 10: 'X', 40: 'XL', 50: 'L' };
var timeChecker = /^([01]\d|2[0-3]):([0-5]\d)$/;
/**
* @param {String} time – время в формате HH:MM (например, 09:05)
* @returns {String} – время римскими цифрами (IX:V)
*/
function romanTime(time) {
// Немного авторского кода и замечательной магии
if (!timeChecker.test(time)) {
throw new TypeError();
}
var hourAndMinute = timeChecker.exec(time);
return convertToRomanTime(hourAndMinute[1] - 0) +
':' + convertToRomanTime(hourAndMinute[2] - 0);
}
function convertToRomanTime(intValue) {
if (intValue === 0) {
return 'N';
}
var usedNumbers = Object.keys(associations);
var stringRepresentation = '';
for (let i = usedNumbers.length - 1; i >= 0; i--) {
while (intValue >= usedNumbers[i]) {
stringRepresentation += associations[usedNumbers[i]];
intValue -= usedNumbers[i];
}
}
return stringRepresentation;
}
module.exports = romanTime;