Advertisement
DNNdrago

11. Check the Brackets

Jul 24th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. function checkBrackets(value) {
  2. var countOfBrackets = 0;
  3. var correct = true;
  4.  
  5. for(var i = 0; i < value.length; i+=1) {
  6. if (value[i] === '(') {
  7. countOfBrackets += 1;
  8. }
  9. else if (value[i] === ')') {
  10. if(countOfBrackets === 0) {
  11. correct = false;
  12. break;
  13. }
  14.  
  15. else {
  16. countOfBrackets -= 1;
  17. }
  18. }
  19. }
  20.  
  21. return (correct && countOfBrackets === 0) ? 'correct' : 'incorrect';
  22.  
  23. }
  24.  
  25. console.log(checkBrackets('( ( a + b ) / 5 – d )'));
  26. console.log(checkBrackets(') ( a + b ) )'));
  27. console.log(checkBrackets('( b * ( c + d *2 / ( 2 + ( 12 – c / ( a + 3 ) ) ) )'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement