Skip to content

Instantly share code, notes, and snippets.

@anushshukla
Created July 23, 2019 09:50
Show Gist options
  • Select an option

  • Save anushshukla/51323ebc9d56df0c3be1862996feb547 to your computer and use it in GitHub Desktop.

Select an option

Save anushshukla/51323ebc9d56df0c3be1862996feb547 to your computer and use it in GitHub Desktop.
Checking opening and close scope
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