Skip to content

Instantly share code, notes, and snippets.

@kenduigraha
Created December 29, 2016 12:09
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 kenduigraha/7d0c925c25c86dcdb50cf7c0a8b4e078 to your computer and use it in GitHub Desktop.
Save kenduigraha/7d0c925c25c86dcdb50cf7c0a8b4e078 to your computer and use it in GitHub Desktop.
function to_roman(input) {
var romans = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
var decimals = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
var result_roman = '';
var result_num = 0
if(isNaN(input) === false){
for(var i = 0 ; i < romans.length ; i++){
while(input >= decimals[i]){
result_roman += romans[i];
input -= decimals[i];
}
}
return result_roman;
}else{
for (var i = 0 ; i <= decimals.length ; i++) {
while(input.indexOf(romans[i]) === 0){
result_num += decimals[i];
input = input.replace(romans[i], '');
}
}
return result_num
}
}
console.log(to_roman("XXXVIII"))
console.log(to_roman(1999))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment