Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for [old] Reverse Roman Numerals by s0me1
"use strict";
function reverseRoman(roman) {
const DICT = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000};
const SUBST = {'CM': 'DCCCC', 'CD': 'CCCC',
'XC': 'LXXXX', 'XL': 'XXXX',
'IX': 'VIIII', 'IV': 'IIII'};
for (const key in SUBST) {
roman = roman.replace(key, SUBST[key]);
}
let num = roman.split('').reduce( (acc, e) => acc + DICT[e], 0);
return num;
}
var assert = require('assert');
if (!global.is_checking) {
assert.equal(reverseRoman('VI'), 6, "First")
assert.equal(reverseRoman('LXXVI'), 76, "Second")
assert.equal(reverseRoman('CDXCIX'), 499, "Third")
assert.equal(reverseRoman('MMMDCCCLXXXVIII'), 3888, "Forth")
console.log("Coding complete, Cesar? Click 'Check' to review your tests and earn cool rewards!");
}
Nov. 2, 2018