• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

204
Views
Why is the number I return undefined?

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);

almost 3 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error