Last active
May 9, 2022 09:31
-
-
Save CyberGen49/ba9c4ac27aadebe2275799170e434f84 to your computer and use it in GitHub Desktop.
Integer to Roman Numerals
This file contains hidden or 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 numeral(num) { | |
const ones = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX']; | |
const tens = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC']; | |
const huns = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM']; | |
return [ | |
'X̅'.repeat(Math.floor(num/10000)), | |
'M'.repeat(Math.floor((num%10000)/1000)), | |
huns[Math.floor((num%1000)/100)], | |
tens[Math.floor(((num%1000)%100)/10)], | |
ones[Math.floor(((num%1000)%100)%10)] | |
].join(''); | |
}; | |
console.log(numeral(12345)); // X̅MMCCCXLV |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment