Files
  • index.js
index.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
31
32
33
34
35
const MDCCLXXVI = 'MDCCLXXVI'; // 1776
const MCMLIV = 'MCMLIV'; // 1954
const MCMXC = 'MCMXC'; // 1990

function toDecimal(numeral) {
  const sym = {
    i: 1,
    v: 5,
    x: 10,
    l: 50,
    c: 100,
    d: 500,
    m: 1000
  };
  return (numeral.toString().toLowerCase().split(''))
    .map(j => sym[j])
    .reverse()  
    .reduce((total, current, i, arr) => {
      return (i === 0 || current >= arr[i-1])
        ? total + current 
        : total - current;
    }, 0);
}

console.log(toDecimal('IV'));
console.log(toDecimal('VIII'));
console.log(toDecimal('IX'));
console.log(toDecimal('XL'));
console.log(toDecimal('XC'));
console.log(toDecimal(MDCCLXXVI));
console.log(toDecimal(MCMLIV));
console.log(toDecimal('MCMLXXIV'));
console.log(toDecimal(MCMXC));
console.log(toDecimal('MCMXCIV'));
node v9.7.1 linux/amd64