Last active
June 20, 2016 14:58
-
-
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
This file contains 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 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