Skip to content

Instantly share code, notes, and snippets.

@b-bot
Created July 25, 2019 08:09
Show Gist options
  • Save b-bot/bcf0ad95f6fe5d1e4ec44874ddd651b4 to your computer and use it in GitHub Desktop.
Save b-bot/bcf0ad95f6fe5d1e4ec44874ddd651b4 to your computer and use it in GitHub Desktop.
Function to convert any number to roman numerals. eg. 10 = X
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