-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (37 loc) · 1.15 KB
/
index.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
const levels = [
{ i: 'I', v: 'V', x: 'X' },
{ i: 'X', v: 'L', x: 'C' },
{ i: 'C', v: 'D', x: 'M' },
];
const calcDigit = (decAt, l) => {
if (l > 2) {
let str = '';
for (let m = 1; m <= decAt * Math.pow(10, l - 3); m++) str += 'M';
return str;
} else if (decAt == 1) return levels[l].i;
else if (decAt == 2) return levels[l].i + levels[l].i;
else if (decAt == 3) return levels[l].i + levels[l].i + levels[l].i;
else if (decAt == 4) return levels[l].i + levels[l].v;
else if (decAt == 5) return levels[l].v;
else if (decAt == 6) return levels[l].v + levels[l].i;
else if (decAt == 7) return levels[l].v + levels[l].i + levels[l].i;
else if (decAt == 8)
return levels[l].v + levels[l].i + levels[l].i + levels[l].i;
else if (decAt == 9) return levels[l].i + levels[l].x;
else return '';
};
/**
*
* @param {number} dec
* dec (decimal number) is required
* and the function will return Roman version of the number
*
*/
const roman = (dec) => {
dec += '';
let romanNum = '';
for (let i = 0; i < dec.length; i++)
romanNum += calcDigit(eval(dec.charAt(i)), dec.length - i - 1);
return romanNum;
};
module.exports = roman;