Skip to content

Instantly share code, notes, and snippets.

@alexnoz
Created December 12, 2017 17:14
Show Gist options
  • Save alexnoz/51a25ffd65408f1f2776eb04cb297b29 to your computer and use it in GitHub Desktop.
Save alexnoz/51a25ffd65408f1f2776eb04cb297b29 to your computer and use it in GitHub Desktop.
A generator function that yields roman numerals in the specified diapason
function* romanSequence (from, to) {
switch (arguments.length) {
case 0:
console.warn('[romanNumerals]: the function expects at least one argument')
return
case 1:
to = from
from = 1
}
const key = [
"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM",
"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC",
"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"
]
for (; from <= to; from++) {
const digits = String(from).split("")
let i = 3
let roman = ""
while (i--)
roman = (key[+digits.pop() + (i * 10)] || "") + roman
yield Array(+digits.join("") + 1).join("M") + roman
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment