I try to solve the Leedcode question 13 and the question is Given a roman numeral, convert it to an integer.(Input is guaranteed to be within the range from 1 to 3999.) This is my code below, I wonder why it only converts the first character in the roman numeral to integer?
var romanToInt = function(s) {
var result = 0;
if (s == null) {
result = 0;
}
var myMap = new Map();
myMap.set('I', 1);
myMap.set('V', 5);
myMap.set('X', 10);
myMap.set('L', 50);
myMap.set('C', 100);
myMap.set('D', 500);
myMap.set('M', 1000);
var len = s.length;
for (var i = 0; i < len; i++) {
if (myMap.get(s.charAt(i)) < myMap.get(s.charAt(i + 1))) {
result -= myMap.get(s.charAt(i))
} else {
result += myMap.get(s.charAt(i))
}
return result;
};
}
console.log(romanToInt('VI'));
console.log(romanToInt('V'));
console.log(romanToInt('VII'));