0

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?

Wyck
  • 10,311
  • 6
  • 39
  • 60
Daniel Z
  • 9
  • 2

1 Answers1

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'));
Evren
  • 4,147
  • 1
  • 9
  • 16