Created
September 22, 2021 08:11
-
-
Save ilhamarrouf/4c07713ea02dea48437475a6382f6108 to your computer and use it in GitHub Desktop.
Convert To Roman Number
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 romanNumber(num) { | |
if (typeof num !== 'number') { | |
return false; | |
} | |
let digits = String(+num).split(""); | |
let key = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM","","X","XX","XXX","XL","L","LX","LXX","LXXX","XC","","I","II","III","IV","V","VI","VII","VIII","IX"]; | |
let romanNum = ""; | |
let i = 3; | |
while (i--) { | |
romanNum = (key[+digits.pop() + (i * 10)] || "") + romanNum; | |
} | |
return Array(+digits.join("") + 1).join("M") + romanNum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment