Created
February 7, 2019 18:35
-
-
Save jackwilsdon/efbaa4f37d05f0993f4c05116a9c34ad 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 arabicToRoman(number) { | |
const letters = [ | |
["M", 1000], | |
["CM", 900], | |
["D", 500], | |
["CD", 400], | |
["C", 100], | |
["XC", 90], | |
["L", 50], | |
["XL", 40], | |
["X", 10], | |
["IX", 9], | |
["V", 5], | |
["IV", 4], | |
["I", 1] | |
]; | |
let roman = ""; | |
while (number > 0) { | |
// Find the largest letter which has a value less than or equal to than the remaining value. | |
const [letter, value] = letters.find(([, value]) => number >= value); | |
roman += letter; | |
number -= value; | |
} | |
return roman; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment