Advertisement
sylviapsh

Correct Brackets Problem (Catalan numbers task group)

May 19th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. function checkBrackets(str){
  3.     var openedCounter = 0;
  4.     var closedCounter =  0;
  5.     var isCorrect = true;
  6.     for (var i = 0; i <= str.length; i++){
  7.         if (str[i] === '('){
  8.           openedCounter++;
  9.         }
  10.         if (str[i] === ')'){
  11.           closedCounter++;
  12.         }
  13.         if (openedCounter < closedCounter){
  14.             return false;        
  15.         }
  16.     }
  17.     if (openedCounter !== closedCounter){
  18.         return false;
  19.     }
  20.    
  21.     return true;
  22. }
  23.  
  24. var testStr = '((a+b)/5-d)';
  25. console.log(testStr);
  26. console.log('Are the brackets in the expression correct? ' + checkBrackets(testStr));
  27.  
  28. var testStr2 = ')(a+b))';
  29. console.log(testStr2);
  30. console.log('Are the brackets in the expression correct? ' + checkBrackets(testStr2));
  31.  
  32. var testStr3 = '()(a+b))(';
  33. console.log(testStr3);
  34. console.log('Are the brackets in the expression correct? ' + checkBrackets(testStr3));
  35. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement