Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. <script type="text/javascript">
  2. function bracketValidator(input){
  3. const splittedInput = input.split('')
  4. const length = splittedInput.length
  5. let depth = 0
  6. if(length % 2 !== 0) return false
  7.  
  8. for(let i = 0; i < length; i++){
  9. // if(splittedInput[i] !== '{' || splittedInput[length-(i+1)] !== '}'){
  10. // return false
  11. // }
  12. if(splittedInput[i] === '{'){
  13. ++depth
  14. } else if(splittedInput[i] === '}'){
  15. --depth
  16. }
  17. if(depth === -1) return false
  18. }
  19. if(depth === 0) return true
  20. return false
  21. }
  22. console.log(bracketValidator('{}}{{}'))
  23. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement