forked from urfu-2016/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
63 lines (53 loc) · 1.7 KB
/
roman-time.js
File metadata and controls
63 lines (53 loc) · 1.7 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
61
62
63
'use strict';
/**
* @param {String} time – время в формате HH:MM (например, 09:05)
* @returns {String} – время римскими цифрами (IX:V)
*/
function romanTime(time) {
if (!isTimeFormatCorrect(time)) {
throwError(8, time);
}
var timeParts = time.split(':');
if (timeParts.length !== 2 || isNaN(timeParts[0]) || isNaN(timeParts[1])) {
throwError(12, time);
}
var hours = parseInt(timeParts[0], 10);
var minutes = parseInt(timeParts[1], 10);
if (isTimeOutOfRange(hours, minutes)) {
throwError(17, time);
}
return getRomanRepresentation(hours) + ':' + getRomanRepresentation(minutes);
}
function isTimeFormatCorrect(time) {
var timeNotEmpty = time !== null && time !== undefined;
var isTimeCorrect = typeof time === 'string' && time.indexOf(':') !== -1;
return timeNotEmpty && isTimeCorrect;
}
function isTimeOutOfRange(hours, minutes) {
return (hours < 0 || hours > 23 || minutes < 0 || minutes > 59);
}
function throwError(line, incomingData) {
throw new TypeError('incorrect data(' + line + '): ' + incomingData);
}
var romanDigits = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'];
function getRomanRepresentation(number) {
if (number === 0) {
return 'N';
}
var result = '';
if (parseInt((number / 50)) > 0) {
result += 'L';
}
number = number % 50;
var xDigitsCount = parseInt(number / 10);
if (xDigitsCount === 4) {
result = 'XL';
} else {
for (var i = 1; i <= xDigitsCount; i++) {
result += 'X';
}
}
result += romanDigits[number % 10];
return result;
}
module.exports = romanTime;