Created
May 27, 2014 14:57
-
-
Save sharnie/4936ae6d3ab167e9e8a9 to your computer and use it in GitHub Desktop.
Convert numbers to Roman Numerals using JavaScript
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 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