Skip to content

Instantly share code, notes, and snippets.

@rakin92
Last active September 5, 2019 05:40
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 rakin92/b5879e202d0de5bbc860009bd493f380 to your computer and use it in GitHub Desktop.
Save rakin92/b5879e202d0de5bbc860009bd493f380 to your computer and use it in GitHub Desktop.
converts number to roman numeral
function toRoman(num) {
let result = '';
const decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
const roman = ["M", "CM","D","CD","C", "XC", "L", "XL", "X","IX","V","IV","I"];
for (let i = 0;i<=decimal.length;i++) {
while (num%decimal[i] < num) {
result += roman[i];
num -= decimal[i];
}
}
return result;
}
toRoman(123); //returns "CXXIII"
toRoman(3290); // returns "MMMCCXC"
toRoman(777); // returns "DCCLXXVII"
toRoman(949); // returns "CMXLIX"
toRoman(2000); // returns "MM"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment