Created
January 16, 2021 08:42
-
-
Save FelixLuciano/2bb9215612135a396f9ad0607b2b7580 to your computer and use it in GitHub Desktop.
A function that takes in a number from 1 to 1000 and returns that number in Roman Numerals.
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
Number.prototype.toRomans = function () { | |
let result = "" | |
for (let [i, alpha] of [...this.toString()].reverse().entries()) { | |
let set = "" | |
if (alpha > 0 && alpha <= 4) set += "IXCM"[i].repeat((alpha - 1) % 3 + 1) | |
if (alpha > 3 && alpha < 9) set += "VLD"[i] | |
if (alpha > 5 && alpha <= 9) set += "IXC"[i].repeat(alpha % 3 + 1) | |
if (alpha === "9") set += "XCM"[i] | |
result = set + result | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment