Last active
January 17, 2016 08:41
-
-
Save michaelmrose/b514ce9e11249bcb3582 to your computer and use it in GitHub Desktop.
oh the romanity
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
function convert(num) { | |
var remainder = num; | |
var result = ""; | |
while(remainder > 0){ | |
var number = closestNumeralWithoutGoingOver(remainder); | |
result = result.concat(returnNumeral(number)); | |
remainder -= number; | |
} | |
return result; | |
} | |
function closestNumeralWithoutGoingOver(val){ | |
var numerals = [1,4,5,9,10,40,50,90,100,400,500,900,1000]; | |
var number = 0; | |
for(var i=0;i<numerals.length;i++){ | |
if (numerals[i]<= val){ | |
number = numerals[i]; | |
} | |
else {break;} | |
} | |
return number; | |
} | |
function returnNumeral(num){ | |
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" | |
}; | |
return ohtheromanity[num]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment