We’re doing lots of interviews at my work and two of my team are doing white-board exercises with applicants. They’re doing the classic “convert a roman numeral to a decimal” problem.

In all my years, ironically, I have never been asked such a thing. So this morning on the train I decided to have a crack at it myself.

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);
}