Last active
November 9, 2015 20:59
-
-
Save DanielKucal/999ba42c23a814bcf1ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function arabic2roman(num) { | |
var romans = { | |
0: {"key": 1, "value": "I"}, | |
1: {"key": 4, "value": "IV" }, | |
2: {"key": 5, "value": "V"}, | |
3: {"key": 9, "value": "IX"}, | |
4: {"key": 10, "value": "X"}, | |
5: {"key": 40, "value": "XL"}, | |
6: {"key": 50, "value": "L"}, | |
7: {"key": 90, "value": "XC"}, | |
8: {"key": 100, "value": "C"}, | |
9: {"key": 400, "value": "CD"}, | |
10: {"key": 500, "value": "D"}, | |
11: {"key": 900, "value": "CM"}, | |
12: {"key": 1000, "value": "M"} | |
}; | |
var roman = ""; | |
while (num > 0) { | |
for (var i=romans.length-1; i>=0; i--) { | |
if (num >= romans[i].key) { | |
roman += romans[i].value; | |
num -= romans[i].key; | |
break; | |
} | |
} | |
} | |
return roman; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment