Creates a function to determine whether curly braces are closed in a given expression.
var expression = “{{}}{}{}”var expressionFalse = “{}{{}”; isBalanced(expression); // trueisBalanced(expressionFalse); // falseisBalanced(“”); // true
Example:
function isBalanced(expression) { var checkString = expression; var stack = [];
// If empty, parentheses are technically balanced if (checkString.length <= 0) return true;
for (var i = 0; i < checkString.length; i++) { if (checkString[i] === ‘{‘) { stack.push(checkString[i]); } else if (checkString[i] === ‘}’) { // Pop on an empty array is undefined if (stack.length > 0) { stack.pop(); } else { return false; }}}
// If the array is not empty, it is not balanced if (stack.pop()) return false; return true; }