Skip to content

Instantly share code, notes, and snippets.

@jackwilsdon
Created February 7, 2019 18:35
Show Gist options
  • Save jackwilsdon/efbaa4f37d05f0993f4c05116a9c34ad to your computer and use it in GitHub Desktop.
Save jackwilsdon/efbaa4f37d05f0993f4c05116a9c34ad to your computer and use it in GitHub Desktop.
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