Skip to content

Instantly share code, notes, and snippets.

@vvgomes
Last active April 1, 2022 17:21
Show Gist options
  • Save vvgomes/07b523f3a698a17d6c82f1a64e6cd89b to your computer and use it in GitHub Desktop.
Save vvgomes/07b523f3a698a17d6c82f1a64e6cd89b to your computer and use it in GitHub Desktop.
const ROMAN_VALUES = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
};
function romanNumeralsToInteger(input, prev = 0, output = 0) {
if (!input || typeof input !== "string") return output;
const curr = ROMAN_VALUES[input[0].toUpperCase()];
if (!curr) return romanNumeralsToInteger("", prev, 0);
const value = prev < curr ? curr - 2 * prev : curr;
return romanNumeralsToInteger(input.slice(1), value, output + value);
}
console.log(romanNumeralsToInteger('I') === 1);
console.log(romanNumeralsToInteger('II') === 2);
console.log(romanNumeralsToInteger('III') === 3);
console.log(romanNumeralsToInteger('IV') === 4);
console.log(romanNumeralsToInteger('V') === 5);
console.log(romanNumeralsToInteger('VI') === 6);
console.log(romanNumeralsToInteger('VII') === 7);
console.log(romanNumeralsToInteger('VIII') === 8);
console.log(romanNumeralsToInteger('IX') === 9);
console.log(romanNumeralsToInteger('X') === 10);
console.log(romanNumeralsToInteger('XI') === 11);
console.log(romanNumeralsToInteger('XII') === 12);
console.log(romanNumeralsToInteger('XIII') === 13);
console.log(romanNumeralsToInteger('XIV') === 14);
console.log(romanNumeralsToInteger('XV') === 15);
console.log(romanNumeralsToInteger('XVI') === 16);
console.log(romanNumeralsToInteger('XVII') === 17);
console.log(romanNumeralsToInteger('XVIII') === 18);
console.log(romanNumeralsToInteger('XIX') === 19);
console.log(romanNumeralsToInteger('XX') === 20);
console.log(romanNumeralsToInteger('CLXXXVIII') === 188);
console.log(romanNumeralsToInteger('CDLVII') === 457);
console.log(romanNumeralsToInteger('DC') === 600);
console.log(romanNumeralsToInteger('CCXCIV') === 294);
console.log(romanNumeralsToInteger('CXXIX') === 129);
console.log(romanNumeralsToInteger('CCLXXIV') === 274);
console.log(romanNumeralsToInteger('CCXIII') === 213);
console.log(romanNumeralsToInteger('CDXXXVII') === 437);
console.log(romanNumeralsToInteger('CMXV') === 915);
console.log(romanNumeralsToInteger('DCCLXI') === 761);
console.log(romanNumeralsToInteger() === 0);
console.log(romanNumeralsToInteger(null) === 0);
console.log(romanNumeralsToInteger([]) === 0);
console.log(romanNumeralsToInteger(-1) === 0);
console.log(romanNumeralsToInteger(200) === 0);
console.log(romanNumeralsToInteger('') === 0);
console.log(romanNumeralsToInteger('CXIA') === 0);
console.log(romanNumeralsToInteger('x') === 10);
console.log(romanNumeralsToInteger('ix') === 9);
console.log(romanNumeralsToInteger('XCVI') === 96);
console.log(romanNumeralsToInteger('xcvi') === 96);
console.log(romanNumeralsToInteger('xCvI') === 96);
console.log(romanNumeralsToInteger('cxii') === 112);
console.log(romanNumeralsToInteger('mcXXXIV') === 1134);
console.log(romanNumeralsToInteger('MMDLXII') === 2562);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment