Created
July 25, 2019 08:09
-
-
Save b-bot/bcf0ad95f6fe5d1e4ec44874ddd651b4 to your computer and use it in GitHub Desktop.
Function to convert any number to roman numerals. eg. 10 = X
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
var numeralCodes = [["","I","II","III","IV","V","VI","VII","VIII","IX"], // Ones | |
["","X","XX","XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"], // Tens | |
["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"]]; // Hundreds | |
function convert(num) { | |
var numeral = ""; | |
var digits = num.toString().split('').reverse(); | |
for (var i=0; i < digits.length; i++){ | |
numeral = numeralCodes[i][parseInt(digits[i])] + numeral; | |
} | |
return numeral; | |
} | |
console.log(convert("10")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment