Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda