Created
October 15, 2019 07:07
-
-
Save jmortensen/dabcca8e451b107d8cf1595121d798cf to your computer and use it in GitHub Desktop.
Example function that will convert a roman numeral to a regular number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// roman numeral mapping | |
const NUMERALS = { | |
I: 1, | |
V: 5, | |
X: 10, | |
L: 50, | |
C: 100, | |
D: 500, | |
M: 1000 | |
}; | |
/** | |
* compares two values to rank them | |
* @param {number} c1 - first value to compare | |
* @param {number} c2 - second value to compare | |
* @returns {number} - returns 0, 1 or -1 if the c1 is equal, greater or less than the c2 | |
*/ | |
const compare = (c1, c2) => { | |
if (NUMERALS[c1] > NUMERALS[c2]) { | |
return 1; | |
} else if (NUMERALS[c1] === NUMERALS[c2]) { | |
return 0; | |
} else if (NUMERALS[c1] < NUMERALS[c2]) { | |
return -1; | |
} | |
}; | |
/** | |
* Given a Roman Numeral, convert it to a number | |
* @param {string} romanNumeral - a valid Roman Numeral | |
* @returns {number} | |
*/ | |
const readRomanNumeral = romanNumeral => { | |
let rnv = 0; | |
const chars = [...romanNumeral]; | |
for (let i = 0; i < chars.length; ) { | |
if (i + 1 <= chars.length) { | |
const curChar = chars[i]; | |
const nextChar = chars[i + 1]; | |
const rank = compare(curChar, nextChar); | |
switch (rank) { | |
case -1: | |
rnv += NUMERALS[nextChar] - NUMERALS[curChar]; | |
i += 2; | |
break; | |
default: | |
rnv += NUMERALS[curChar]; | |
i++; | |
} | |
} else { | |
rnv += NUMERALS[chars[i]]; | |
i++; | |
} | |
} | |
return rnv; | |
}; | |
// const tests = { | |
// I: 1, | |
// II: 2, | |
// III: 3, | |
// IV: 4, | |
// VII: 7, | |
// XI: 9, | |
// XIII: 13, | |
// XIV: 14, | |
// XX: 20, | |
// XLVII: 47, | |
// MCD: 1400, | |
// MCMXIV: 1914 | |
// }; | |
// Object.entries(tests).forEach(([k, v]) => { | |
// console.log(`reading: ${k} value should be ${v} got: ${readRomanNumeral(k)}`); | |
// }); | |
const tests = [ | |
{ | |
expected: 1, | |
test: "I" | |
}, | |
{ | |
expected: 5, | |
test: "V" | |
}, | |
{ | |
expected: 9, | |
test: "IX" | |
}, | |
{ | |
expected: 9, | |
test: "VIIII" | |
}, | |
{ | |
expected: 10, | |
test: "X" | |
}, | |
{ | |
expected: 14, | |
test: "XIV" | |
}, | |
{ | |
expected: 19, | |
test: "XIX" | |
}, | |
{ | |
expected: 50, | |
test: "L" | |
}, | |
{ | |
expected: 89, | |
test: "LXXXIX" | |
}, | |
{ | |
expected: 90, | |
test: "XC" | |
}, | |
{ | |
expected: 100, | |
test: "C" | |
}, | |
{ | |
expected: 499, | |
test: "ID" | |
}, | |
{ | |
expected: 500, | |
test: "D" | |
}, | |
{ | |
expected: 1000, | |
test: "M" | |
}, | |
{ | |
expected: 2, | |
test: "II" | |
}, | |
{ | |
expected: 7, | |
test: "VII" | |
}, | |
{ | |
expected: 9, | |
test: "IX" | |
}, | |
{ | |
expected: 20, | |
test: "XX" | |
}, | |
{ | |
expected: 505, | |
test: "DV" | |
}, | |
{ | |
expected: 919, | |
test: "CMXIX" | |
} | |
]; | |
const results = tests.map(t => { | |
const result = readRomanNumeral(t.test); | |
return { | |
passes: result === t.expected, | |
expected: t.expected, | |
result, | |
test: t.test | |
}; | |
}); | |
const testResults = results.filter(t => { | |
return !t.passes; | |
}); | |
results.forEach(tr => { | |
console.log(tr); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment