-
Notifications
You must be signed in to change notification settings - Fork 1
/
roman-numerals.js
30 lines (24 loc) · 1.08 KB
/
roman-numerals.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const parseDigit = (n, order) =>
n >= Math.pow(10, order) ? Math.floor((n / Math.pow(10, order)) % 10) : 0;
const formatDigit = (digit, remSymbol, fivesSymbol, greaterOrderSymbol) => {
const extractRem = d => d % 5;
const formatRem = rem =>
rem !== 4
? Array.from({ length: rem }, () => remSymbol).join("")
: `${remSymbol}${fivesSymbol}`;
const extractFives = d => (d >= 5 ? 1 : 0);
const formatFives = fives => (fives ? fivesSymbol : "");
const createRemString = () => formatRem(extractRem(digit));
const createFivesString = () => formatFives(extractFives(digit));
const createNineString = () => `${remSymbol}${greaterOrderSymbol}`;
return digit !== 9
? `${createFivesString()}${createRemString()}`
: createNineString();
};
export const toRoman = n => {
const thousands = formatDigit(parseDigit(n, 3), "M", "", "");
const hundreds = formatDigit(parseDigit(n, 2), "C", "D", "M");
const tens = formatDigit(parseDigit(n, 1), "X", "L", "C");
const ones = formatDigit(parseDigit(n, 0), "I", "V", "X");
return `${thousands}${hundreds}${tens}${ones}`;
};