Skip to content

Instantly share code, notes, and snippets.

@FelixLuciano
Created January 16, 2021 08:42
Show Gist options
  • Save FelixLuciano/2bb9215612135a396f9ad0607b2b7580 to your computer and use it in GitHub Desktop.
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.
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