Skip to content

Commit a829853

Browse files
authored
Create romannumeralconverter.js
1 parent 6c9aec3 commit a829853

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function convertToRoman(num) {
2+
const arabicNum = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
3+
const romanNum = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
4+
5+
let result = ""
6+
for (let i=0 ; i<arabicNum.length ; i++) {
7+
while (num >= arabicNum[i]) {
8+
result += romanNum[i]
9+
num -= arabicNum[i]
10+
}
11+
}
12+
13+
return result;
14+
}
15+
16+
convertToRoman(4000);

0 commit comments

Comments
 (0)