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

207
Views
string.substring() in Javascript

const removeParenth = function (str) {
  // your code here - don't forget to return a string!
  let start;
  let finish;
  for (let i in str) {
    if (str[i] === '(') {
      start = i;
    }
    if (str[i] === ')') {
      finish = i;
    }
  }
  //   console.log(start, finish);
  //   console.log(str);
  let omitStr = str.substring(start, finish + 1);
  console.log(omitStr);
  return str.replace(omitStr, "");
};

console.log(removeParenth('ido(not)liketocode'));

I'm trying to slice the '(not)' part from the input string, but somehow the 'omitStr' gives me '(not)liketocode' instead of '(not)'.

Anyone knows why this happens?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Because you are summing string and number so it gives you uncorrect result. You can change this line finish = parseInt(i);

const removeParenth = function (str) {
  // your code here - don't forget to return a string!
  let start;
  let finish;
  for ( let i in str) {
      if (str[i] === '(') {
          start = parseInt(i);
      }
      if (str[i] === ')') {
          finish = parseInt(i);
      }
  }
//   console.log(start, finish);
//   console.log(str);
  let omitStr = str.substring(start, finish+1);
  console.log(omitStr);
  return str.replace(omitStr, "");
};

console.log(removeParenth('ido(not)liketocode'));

about 4 years ago · Juan Pablo Isaza Report
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!