-
Notifications
You must be signed in to change notification settings - Fork 0
/
decToRoman.js
58 lines (46 loc) · 1.28 KB
/
decToRoman.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
var mappingHash = {
1000: { 1: 'M' },
100: { 1: 'C', 5: 'D' },
10: { 1: 'X', 5: 'L' },
1: { 1: 'I', 5: 'V' }
};
var keys = Object.keys(mappingHash);
keys.sort().reverse();
var decToRoman = function(input) {
var dec = parseInt(input, 10);
if (dec != input || dec <= 0 || dec >= 4000) {
throw new Error('Unsupported input');
}
var rem = dec,
i = 0,
response = '',
ones,
keyInt;
for (; i < keys.length; i++) {
keyInt = parseInt(keys[i], 10);
if (rem < keyInt) { continue; }
ones = Math.floor(rem/keyInt);
if (ones == 9) {
response += get(1, 1, keyInt) + get(1, 1, keys[i-1]);
} else if (ones >= 5) {
response += get(1, 5, keyInt) + get(ones - 5, 1, keyInt);
} else if (ones == 4) {
response += get(1, 1, keyInt) + get(1, 5, keyInt);
} else if (ones < 4) {
response += get(ones, 1, keyInt);
} else {
throw new Error('Internal error.');
}
rem = rem % keyInt;
}
return response;
};
var get = function(how, what, where) {
var res = '';
while(how--){
res += mappingHash[where][what];
}
return res;
};
module.exports = decToRoman;