Skip to content

Instantly share code, notes, and snippets.

@sharnie
Created May 27, 2014 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharnie/4936ae6d3ab167e9e8a9 to your computer and use it in GitHub Desktop.
Save sharnie/4936ae6d3ab167e9e8a9 to your computer and use it in GitHub Desktop.
Convert numbers to Roman Numerals using JavaScript
var toRoman = function(num){
var range, numerals, roman_numeral, i;
range = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
numerals = "M CM D CD C XC L XL X IX V IV I".split(" ");
roman_numeral = "";
for(i=0;i<range.length;i++){
while(num >= range[i]){
num -= range[i];
roman_numeral += numerals[i];
}
}
return roman_numeral;
};
return toRoman;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment