Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!