-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertNumberToRoman_v2.js
54 lines (47 loc) · 1.05 KB
/
convertNumberToRoman_v2.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
// base roman-numeral map
const table = {
1: 'I',
5: 'V',
10: 'X',
50: 'L',
100: 'C',
500: 'D',
1000: 'M',
};
const tableEntity = Object.fromEntries([
[0, Object.values(table).slice(0, 3)],
[1, Object.values(table).slice(2, 5)],
[2, Object.values(table).slice(4)],
]);
function getRomaOfDigit(digit, level) {
// hack
if (level === 3) return 'M'.repeat(digit);
const [low, middle, high] = [...tableEntity[level]];
switch (true) {
case digit === 9:
return low + high;
case digit >= 5:
return middle + low.repeat(digit - 5);
case digit === 4:
return low + middle;
case digit >= 1:
return low.repeat(digit - 1);
default:
return '';
}
}
function convertToRoman(num) {
let res = '',
currentLevel = 4,
rest = num;
while (currentLevel > 0) {
currentLevel--;
base = Math.pow(10, currentLevel);
const digit = Math.floor(rest / base);
res += getRomaOfDigit(digit, currentLevel);
rest = num % base;
}
return res;
}
const res = convertToRoman(44);
console.log(res);