balanced parentheses javascript code example
Example: check for balanced parentheses in javascript
function Check()
{
let s = [];
let pObj = {'{':'}','(':')','[':']'}
let sTop = "";
for(let i=0;i<str.length;i++)
{
if(str[i] == '{' || str[i] == '(' || str[i] == '[')
{
s.push(str[i]);
}
else
{
sTop = s.pop();
if(str[i] !== pObj[sTop])
{
return 0;
}
}
}
return 1;
}
var str = userInput[0];
res = Check(str);
if(res == 0)
{
console.log("Unbalanced");
}
else
{
console.log("Balanced");
}