Skip to content

Instantly share code, notes, and snippets.

@michaelmrose
Last active June 20, 2016 14:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelmrose/c7031aa7d32d926a9ccc to your computer and use it in GitHub Desktop.
Save michaelmrose/c7031aa7d32d926a9ccc to your computer and use it in GitHub Desktop.
an attempt at writing a recursive roman numeral converter that would benefit from tco
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,str="") {
if (n === 0){return str;}
else {
var largestInclusiveNumber = numerals.filter(function(x){return x<=n;}).pop();
var remainder = n -= largestInclusiveNumber;
return convert(remainder,str.concat(ohtheromanity[largestInclusiveNumber]));
}
}
convert(1);
//convert(15000000,"");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment