Skip to content

Instantly share code, notes, and snippets.

@michaelmrose
Last active January 17, 2016 08:41
Show Gist options
  • Save michaelmrose/b514ce9e11249bcb3582 to your computer and use it in GitHub Desktop.
Save michaelmrose/b514ce9e11249bcb3582 to your computer and use it in GitHub Desktop.
oh the romanity
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