Conditions of the problem
Implement and export a default function that accepts a string consisting of only opening and closing parentheses as input and checks whether this string is valid. An empty string (no parentheses) is considered valid.
A string is considered correct (balanced) if the bracket structure contained in it meets the requirements:
Скобки — это парные структуры. У каждой открывающей скобки должна быть соответствующая ей закрывающая скобка. Закрывающая скобка не должна идти впереди открывающей.import areBracketsBalanced from 'roundBracketsValidator';
areBracketsBalanced ('(())'); // true areBracketsBalanced ('((())'); // false
My solution is not validated. Writes const str7 = '()) (()';
37 | expect (areBracketsBalanced (str7)). toBe (false); My code below
const areBracketsBalanced = (str) => { let leftBrackets = ''; let rightBrackets = ''; switch(str[0]) { case ')': return false; break; case '': return true; break; }; if (str[str.length - 1] === '(') { return false; }; for (let i = 0; i < str.length; i++) { if (str[i] === '(') { leftBrackets += str[i]; } else if (str[i] === ')') { rightBrackets += str[i]; } }; if (leftBrackets.length === rightBrackets.length) { return true; } else { return false; } }; export default areBracketsBalanced; What can be fixed?