A Roman numeral writes a number as a run of letters — I, V, X, L, C, D, M — each worth a fixed amount, read left to right and added up. The one twist: when a smaller-valued letter sits directly before a larger one, it's subtracted instead. So VI is 6 (5 + 1) but IV is 4 (5 − 1). You've seen these on clock faces, movie copyright dates, and Super Bowl logos.
Implement romanToInteger(s) that takes a valid Roman numeral string and returns its integer value.
function romanToInteger(s) {
// s is a valid Roman numeral (1–3999)
// returns the integer it represents
}
The seven symbols and their values:
I = 1 V = 5 X = 10 L = 50
C = 100 D = 500 M = 1000
romanToInteger('III'); // 3 (1 + 1 + 1)
romanToInteger('IX'); // 9 (10 − 1)
romanToInteger('LVIII'); // 58 (50 + 5 + 1 + 1 + 1)
romanToInteger('MCMXCIV'); // 1994 (1000 + 900 + 90 + 4)
IV (4), IX (9), XL (40), XC (90), CD (400), CM (900). Every subtraction is a single small symbol before a single larger one.