DEV Community

Discussion on: Daily Challenge #209 - Roman Numerals

Collapse
 
sabbin profile image
Sabin Pandelovitch

JS solution

const v = {
  I: 1,
  V: 5,
  X: 10,
  L: 50,
  C: 100,
  D: 500,
  M: 1000
};

const solution = nr => nr.split("").reduce((acc,cur,i,oa)=> {
  acc = i<oa.length-1 ? (v[cur] >= v[oa[i+1]] ? acc+v[cur] : acc - v[cur]) : acc+ v[cur];
  return acc;
},0);