JSFiddle - React, Tailwind, and code Playground
by alirokni
HTML
<h1>Stack and Queue</h1>
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
JavaScript
function parChecker(symbolString) {
var s = [],
balanced = true,
index = 0;
while (index < symbolString.length && balanced) {
var symbol = symbolString[index];
if (symbol == "(") {
//s.push(symbol); // add to the end of array similar to queue
s.unshift(symbol); // add to the beginning of array similar to stack
} else {
if (s.length === 0) {
balanced = false;
} else {
// s.pop(); // remove from the end of array similar to queue
s.shift(symbol); // remove from the beginning of array similar to stack
}
}
index = index + 1;
console.log(s + " " + s.length);
}
if (balanced && s.length === 0) {
return true;
} else {
return false;
}
}
document.getElementById('one').innerHTML = parChecker('((()))');
document.getElementById('two').innerHTML = parChecker('))');
document.getElementById('three').innerHTML = parChecker('((())');