Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- function checkBrackets(str){
- var openedCounter = 0;
- var closedCounter = 0;
- var isCorrect = true;
- for (var i = 0; i <= str.length; i++){
- if (str[i] === '('){
- openedCounter++;
- }
- if (str[i] === ')'){
- closedCounter++;
- }
- if (openedCounter < closedCounter){
- return false;
- }
- }
- if (openedCounter !== closedCounter){
- return false;
- }
- return true;
- }
- var testStr = '((a+b)/5-d)';
- console.log(testStr);
- console.log('Are the brackets in the expression correct? ' + checkBrackets(testStr));
- var testStr2 = ')(a+b))';
- console.log(testStr2);
- console.log('Are the brackets in the expression correct? ' + checkBrackets(testStr2));
- var testStr3 = '()(a+b))(';
- console.log(testStr3);
- console.log('Are the brackets in the expression correct? ' + checkBrackets(testStr3));
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement