-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.ts
29 lines (29 loc) · 895 Bytes
/
solution.ts
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
function intToRoman (num: number): string {
const l1 = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', ];
const l2 = ['X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', ];
const l3 = ['C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', ];
const l4 = ['M', 'MM', 'MMM', ];
const result:string[] = [];
if (num > 999) {
const rest = num % 1000;
const count = (num - rest) / 1000;
result.push(l4[count - 1]);
num = rest;
}
if (num > 99) {
const rest = num % 100;
const count = (num - rest) / 100;
result.push(l3[count - 1]);
num = rest;
}
if (num > 9) {
const rest = num % 10;
const count = (num - rest) / 10;
result.push(l2[count - 1]);
num = rest;
}
if (num > 0) {
result.push(l1[num - 1]);
}
return result.join('');
}