Skip to content

Instantly share code, notes, and snippets.

@ilhamarrouf
Created September 22, 2021 08:11
Show Gist options
  • Save ilhamarrouf/4c07713ea02dea48437475a6382f6108 to your computer and use it in GitHub Desktop.
Save ilhamarrouf/4c07713ea02dea48437475a6382f6108 to your computer and use it in GitHub Desktop.
Convert To Roman Number
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