Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

256
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda