Additional solution for Rosetta Code: Balanced brackets

What is your hint or solution suggestion?

The only thing that needs to be known is that for balanced brackets, one can keep repeatedly removing [] in the string and end up with a blank string. There will never be ] as the first character or [ as the last character.

Solution 1
function isBalanced(str) {
  while (true) {
    str = str.replace('[]', ''); 
    if(str.length==0){
      return true;
    }
    if(str[0]==']'||str[str.length-1]=='['){
      return false;
    }
  }
}

Challenge: Balanced brackets

Link to the challenge:

isn’t this always undefined?

also this solution doesn’t use recursion so I’m not sure that saying “recursively” is appropriate

for the reason above, I don’t think your solution can be added to the guide. I am unlisting this tooic but leaving it open in case you want to propose a different solution

@ILM I simply replaced str[-1] with str[str.length-1], and replaced “recursively” with “repeatedly”. It now works for all cases.