Skip to content

Instantly share code, notes, and snippets.

@EQuimper
Last active May 19, 2018 17:51
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 EQuimper/695016a19dfe6e5bff916a1d483647af to your computer and use it in GitHub Desktop.
Save EQuimper/695016a19dfe6e5bff916a1d483647af to your computer and use it in GitHub Desktop.
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);
@misterhtmlcss
Copy link

misterhtmlcss commented May 19, 2018

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 :))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment