Simple parenthesis removal - JavaScript I solved the following problem:

You will be given a mathematical string and your task will be to remove all braces as follows:
solve(&quot;x-(y+z)&quot;) = &quot;x-y-z&quot;`

solve(&quot;x-(y-z)&quot;) = &quot;x-y+z&quot;`

solve(&quot;u-(v-w-(x+y))-z&quot;) = &quot;u-v+w+x+y-z&quot;`

solve(&quot;x-(-y-z)&quot;) = &quot;x+y+z&quot;`

There are no spaces in the expression. Only two operators are given: &quot;+&quot; or &quot;-&quot;.

This is the solution I came up with:
function removeAt(s, index) {
    if (index === s.length - 1) {
        return s.slice(0, -1)
    }
    return s.slice(0, index) + s.slice(index + 1);
}

function insertAt(s, index, str) {
    return s.slice(0, index) + str + s.slice(index);
}

function replaceAt(s, index, chr) {
    if (index &gt; s.length - 1) return s;
    return s.slice(0, index) + chr + s.slice(index + 1);
}

function reverseSigns(s, start, end) {
    for (var i = start; i &lt; end; i++) {
        if (s[i] === '-') {
            s = replaceAt(s, i, '+');
        } else if (s[i] === '+') {
            s = replaceAt(s, i, '-');
        }
    }
    return s;
}

function solve(s) {
    //Get the position of the parenthesis 
    var Close = 0, Open = -1;
    while (s[Close] !== ')' &amp;&amp; Close &lt; s.length) {
        if (s[Close] === '(') {
            Open = Close;
        }
        Close++;
    }
    //Check to see if there is any parenthesis 
    if (Open !== -1) {
        //Insert sign before if there is none
        if (s[Open + 1] !== '+' &amp;&amp; s[Open + 1] !== '-') {
            s = insertAt(s, Open + 1, '+');
            //'Close' parenthesis has changed --&gt; +1 right
            Close++;
        }
        //Check to see if a sign has to be replaced 
        if (s[Open - 1] === '-') {
            s = reverseSigns(s, Open + 1, Close - 1);
        }
        //Remove the parenthesis 
        s = removeAt(s, Close);
        s = removeAt(s, Open);
        //Remove the sign before paranthesis
        if (s[Open - 1] === '-' || s[Open - 1] === '+') {
            s = removeAt(s, Open - 1);
        }
        //Recursive call
        return solve(s);
        //return s;
    }
    //Remove unnecessary +
    if (s[0] === '+') {
        s = removeAt(s, 0);
    }
    return s;
}

I was wondering if my code could be improved.
I would do this by iterating through the tokens and keeping a stack of the current state (signStack) as parenthesis are opened.



function newSign(current, prev) {
  //treat blank as positive
  const currentNegative = current[0] == '-';
  const prevNegative    = prev[0]    == '-';
  return currentNegative == prevNegative ? '+' : '-';
}

function solve(input) {
  let signStack   = ['+']; // start by assuming positive
  let currentSign = '';
  
  const result = input.replace(/(\+|\-)\(?|\)|\w/g, (match) =&gt; {
    if (match==')') { // close parenthesis
      signStack.shift(); 
      currentSign = signStack[0];
      return '';
    } 
    
    if (match[0] != '+' &amp;&amp; match[0] != '-') { // variable
      const  sign = currentSign;
      currentSign = signStack[0];;
      return sign + match;
    }
    
    const sign     = newSign(match, currentSign);
    currentSign    = sign;
    if (match.length &gt; 1) {// open parenthesis
      signStack.unshift(sign);
    }
    return '';
  });  
  
  if (signStack.length != 1)
    throw 'unbalanced parenthesis';
    
  return result;  
}

function test(input, expected) {
  const result = solve(input);
  if (result != expected)
    console.log(`Failed: input=${input}, result=${result}, expected=${expected}`); 
}

test("x-(y+z)",        "x-y-z");
test("x-(y-z)",        "x-y+z");
test("u-(v-w-(x+y))-z", "u-v+w+x+y-z");
test("x-(-y-z)",        "x+y+z");