Skip to content

Commit a181f55

Browse files
authoredAug 21, 2021
Create RomanNumeralConverter.js
1 parent 436746e commit a181f55

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
 

‎RomanNumeralConverter.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const numMap = {
2+
1: 'I',
3+
4: 'IV',
4+
5: 'V',
5+
9: 'IX',
6+
10: 'X',
7+
40: 'XL',
8+
50: 'L',
9+
90: 'XC',
10+
100: 'C',
11+
400: 'CD',
12+
500: 'D',
13+
900: 'CM',
14+
1000: 'M'
15+
}
16+
/* I = 1 V = 5 X = 10 L = 50 C = 100 D = 500 M = 1000 */
17+
function convertToRoman(num) {
18+
let result = ''
19+
Object.keys(numMap).reverse().forEach((key) => {
20+
while (num >= key){
21+
result += numMap[key];
22+
num -= key;
23+
}
24+
})
25+
return result;
26+
}
27+
28+
console.log(convertToRoman(9));

0 commit comments

Comments
 (0)