forked from urfu-2016/javascript-task-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
82 lines (76 loc) · 2.32 KB
/
test.html
File metadata and controls
82 lines (76 loc) · 2.32 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<script>
var ONE = 'I';
var FIVE = 'V';
var TEN = 'X';
var FIFTY = 'L';
function checkMinutesArgument(minutes) {
if (isNaN(minutes) || minutes < 0 || minutes > 59) {
throw new TypeError();
}
}
function checkHoursArgument(hours) {
if (isNaN(hours) || hours < 0 || hours > 23) {
throw new TypeError();
}
}
/**
* @param {String} time – время в формате HH:MM (например, 09:05)
* @returns {String} – время римскими цифрами (IX:V)
*/
function romanTime(time) {
var splittedTime = time.split(':');
if (splittedTime.length !== 2 || splittedTime[0].length != 2 || splittedTime[1].length != 2) {
throw new TypeError();
}
var hours = parseInt(splittedTime[0]);
var minutes = parseInt(splittedTime[1]);
checkMinutesArgument(minutes);
checkHoursArgument(hours);
var convertedMinutes = convertToRoman(minutes);
var convertedHours = convertToRoman(hours);
return (convertedHours + ':' + convertedMinutes);
}
function convertToRoman(number) {
var decadesCount = Math.floor(number / 10);
var onesCount = number % 10;
if (decadesCount === 0) {
return digitToRoman(onesCount);
}
if (onesCount === 0) {
return digitToRoman(decadesCount, true);
}
return digitToRoman(decadesCount, true) + digitToRoman(onesCount);
}
function processNoRemainder(isDecade, digit) {
if (digit === 5) {
return isDecade ? FIFTY : FIVE;
}
return 'N';
}
function processMinusOneRemainder(isDecade, digit) {
if (digit > 5) {
return isDecade ? 'NotImplenented' : ONE + TEN;
}
return isDecade ? TEN + FIFTY : ONE + FIVE;
}
function processDefaultRemainder(isDecade, digit, remainder) {
if (digit > 5) {
return isDecade ? FIFTY + TEN.repeat(remainder) : FIVE + ONE.repeat(remainder);
}
return isDecade ? TEN.repeat(remainder) : ONE.repeat(remainder);
}
function digitToRoman(digit, isDecade) {
if (typeof(isDecade) === 'undefined') {
isDecade = false;
}
var remainder = digit % 5;
switch (remainder) {
case 0:
return processNoRemainder(isDecade, digit);
case 4:
return processMinusOneRemainder(isDecade, digit);
default:
return processDefaultRemainder(isDecade, digit, remainder);
}
}
</script>