Last active
May 19, 2018 17:51
-
-
Save EQuimper/695016a19dfe6e5bff916a1d483647af to your computer and use it in GitHub Desktop.
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 normalFormat = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000]; | |
var romanFormat = ['I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M']; | |
function convertToRoman(num) { | |
if (num === 0) return ''; | |
var value = ''; | |
for (var i = 0; i < normalFormat.length; i++) { | |
if (num >= normalFormat[i]){ | |
value = ''; | |
value = romanFormat[i]; | |
} | |
} | |
var index = romanFormat.indexOf(value); | |
var subValue = normalFormat[index]; | |
return value + convertToRoman(num - subValue); | |
} | |
convertToRoman(6); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ironically I feel like yours is more sophisticated than mine. I like it though, because it solves the problem in a very different way from me. I'll send you mine via gist too. It's fun to share code on GH :))