Skip to content

Instantly share code, notes, and snippets.

@michaelmrose
Created January 18, 2016 15:47
Show Gist options
  • Save michaelmrose/3f8c8fe1ac38b5dbd13e to your computer and use it in GitHub Desktop.
Save michaelmrose/3f8c8fe1ac38b5dbd13e to your computer and use it in GitHub Desktop.
recursive solution to roman numeral generator
var numerals = [1,4,5,9,10,40,50,90,100,400,500,900,1000];
var ohtheromanity = {1: "I", 4: "IV", 5:"V", 9: "IX", 10: "X", 40: "XL", 50: "L",
90: "XC", 100: "C", 400: "CD", 500: "D", 900: "CM", 1000: "M"};
function convert(n) {
if (n === 0){return "";}
else {
var largestInclusiveNumber = numerals.sort(function(x,y){return x-y;}).filter(function(x){return x<=n;}).pop();
var remainder = n -= largestInclusiveNumber;
return ohtheromanity[largestInclusiveNumber].concat(convert(remainder));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment