I can't understand why this returns the "total" variable as "undefined." I assume that's why printing out the "number" variable doesn't yield anything useful either.
This is my first question on Leetcode, and I literally rewrote the bottom half of my answer to match the format of someone else's solution, but it still gives me the exact same "undefined" error. Did I do something on top to cause this problem?
var s = "CXVII";
let stringToArray = s.split("");
var mappedArray = stringToArray.map(iterate);
function iterate(value) {
if (value == "I") {
num = 1;
return num;
}
if (value == "V") {
num = 5;
return num;
}
if (value == "X") {
value = 10;
return value;
}
if (value == "L") {
value = 50;
return value;
}
if (value == "C") {
value = 100;
return value;
}
if (value == "D") {
value = 500;
return value;
}
if (value == "M") {
value = 1000;
return value;
}
};
var number = function(mappedArray) {
var total = 0;
for (var i = 0; i < mappedArray.length; i++) {
if (mappedArray[i + 1] > mappedArray[i]) {
total = mappedArray[i + 1] - mappedArray[1];
continue;
}
total += mappedArray[i];
}
return total;
};
console.log(total);