Struggling with Leedcode question 13: Given a roman numeral, convert it to an integer. The input is guaranteed to be within the range from 1 to 3999. I've written some code below, but I'm puzzled as to why it only converts the first character in the roman numeral to an 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'));