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
60 lines (52 loc) · 1.47 KB
/
roman-time.js
File metadata and controls
60 lines (52 loc) · 1.47 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
/* eslint-disable linebreak-style */
'use strict';
let fontAr = [1, 4, 5, 9, 10, 40, 50];
let fontRom = ['I', 'IV', 'V', 'IX', 'X', 'XL', 'L'];
function toRoman(time, number) {
let rezult = '';
let n = number;
while (time > 0) {
if (time >= fontAr[n]) {
rezult += fontRom[n];
time -= fontAr[n];
} else {
n--;
}
}
return rezult;
}
function bolMass(mas) {
let bol = false;
if ((mas[0] !== '') && (mas.length === 2) && (mas[1] !== '')) {
if ((mas[0].length <= 2) && (mas[1].length <= 2)) {
bol = true;
}
}
return bol;
}
/**
* @param {String} time – время в формате HH:MM (например, 09:05)
* @returns {String} – время римскими цифрами (IX:V)
*/
function romanTime(time) {
// Немного авторского кода и замечательной магии
let mas = time.split(':');
let clock = Number(mas[0]);
let minutes = Number(mas[1]);
let strClock = 'N';
let strMinutes = 'N';
let result = '';
if (bolMass(mas) && (clock < 24) && (minutes < 60) && (clock >= 0) && (minutes >= 0)) {
if (clock > 0) {
strClock = toRoman(clock, 4);
}
if (minutes > 0) {
strMinutes = toRoman(minutes, 6);
}
result = strClock + ':' + strMinutes;
} else {
throw new TypeError();
}
return result;
}
module.exports = romanTime;