Created
December 11, 2018 14:51
-
-
Save iCaspar/0c161b342bf038ac48f05455b751449f to your computer and use it in GitHub Desktop.
A function to convert an aribic (base 10) number to a Roman numeral
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 convertToRoman(num) { | |
const romanSymbols = { | |
M: 1000, | |
CM: 900, | |
D: 500, | |
CD: 400, | |
C: 100, | |
XC: 90, | |
L: 50, | |
XL: 40, | |
X: 10, | |
IX: 9, | |
V: 5, | |
IV: 4, | |
I: 1 | |
} | |
let romanNum = '' | |
for (let romanSymbol in romanSymbols) { | |
while (num >= romanSymbols[romanSymbol]) { | |
romanNum += romanSymbol | |
num -= romanSymbols[romanSymbol] | |
} | |
} | |
return romanNum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment