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

251
Vistas
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 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