Created
July 23, 2019 09:50
-
-
Save anushshukla/51323ebc9d56df0c3be1862996feb547 to your computer and use it in GitHub Desktop.
Checking opening and close scope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function isOpeningTag(char) { | |
| return ['{', '[', '('].includes(char) | |
| } | |
| function isClosingTag(char) { | |
| return ['}', ']', ')'].includes(char) | |
| } | |
| function checkScope(str){ | |
| var openingTagsCount = 0; | |
| for (var i = 0; i < str.length; i++){ | |
| var char = str[i]; | |
| if (isClosingTag(char) && !openingTagsCount) { | |
| return false; | |
| } | |
| /* console.log(char, isOpeningTag(char)) */; | |
| if (isOpeningTag(char)) { | |
| openingTagsCount++; | |
| } | |
| if (isClosingTag(char)) { | |
| openingTagsCount--; | |
| } | |
| } | |
| return openingTagsCount === 0; | |
| } | |
| console.log(checkScope('{{}}')); // true expected | |
| console.log(checkScope('{[{}]}')); // true expected | |
| console.log(checkScope('{[{]}')); // true expected | |
| console.log(checkScope('{{}'));// true expected | |
| console.log(checkScope('}{')); // true expected |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment