Created
December 6, 2017 23:42
-
-
Save PantherHawk/59bdf44393c3d276fb6eda93a864c7b4 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 intToRoman = function(num) { | |
// store numerals | |
let romanNums = ''; | |
// make a graph mapping an array of integers to an array of roman numerals such that an index will map the two as we iterate through the integers array. | |
let integers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; | |
let romans = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'] | |
// iterate over integers | |
for (var i = 0; i < integers.length; i++) { | |
// while the remainder of the input number divided by the roman numeral's integer is greater than than the integer at i ... | |
while(num%integers[i] < num) { | |
// subtract the integer at i from the number | |
num -= integers[i]; | |
// add the roman numeral to the string | |
romanNums += romans[i]; | |
} | |
} | |
// return the string | |
return romanNums; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment