Created
December 12, 2017 17:14
-
-
Save alexnoz/51a25ffd65408f1f2776eb04cb297b29 to your computer and use it in GitHub Desktop.
A generator function that yields roman numerals in the specified diapason
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
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