Created
December 29, 2016 12:09
-
-
Save kenduigraha/7d0c925c25c86dcdb50cf7c0a8b4e078 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
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