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

252
Visualizações
Why is javascript replace not working on my string?

Below is a Leetcode problem where I'm turning the input of Roman numbers to numbers. Once I count my larger numerals, I'd like to remove them from the string, but replace is not working. In my debugger, I can confirm that the total is being updated so the if statements are working. I can't seem to figure out why the string is not being updated. Any ideas?

var romanToInt = function (s) {
  let total = 0
  if (s.indexOf("CM") !== -1) {
    total = total + 900
    s.replace('CM', '')
  }
  if (s.indexOf("CD") !== -1) {
    total = total + 400
    s.replace('C', '')
  }
  if (s.indexOf("XC") !== -1) {
    total = total + 90
    s.replace("XC", " ")
  }
  if (s.indexOf("XL") !== -1) {
    total = total + 40
    s.replace("XL", " ")
  }
  if (s.indexOf("IX") !== -1) {
    total = total + 9
    s.replace("IX", " ")
  }
  if (s.indexOf("IV") !== -1) {
    total = total + 4
    s.replace("IV", " ")
  }

  for (i = 0; s.length > i; i++) {

    let position = s[i]

    switch (position) {
      case 'I':
        total = total + 1
        break;
      case 'V':
        total = total + 5
        break
      case 'X':
        total = total + 10
        break;
      case 'L':
        total = total + 50
        break;
      case 'C':
        total = total + 100
        break;
      case 'D':
        total = total + 500
        break;
      case 'M':
        total = total + 1000
        break;
      default:
        total = total + 0;
    }
  }

  return 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