Javascript Journal: Using Stacks to check Unbalanced Parentheses.

Ugoaneni VictorUgoaneni Victor
3 min read

Prerequisite Knowledge:

  • JavaScript fundamentals.

  • Object-Oriented JavaScript.

  • Data Structures and Algorithms.

A stack is an important construct in Data Structures and computer programming in general. The compiler uses it for syntactic analysis, and the processor also utilizes the stack to execute machine-level instructions. As a Last-in, First-out(LIFO) Data Structure, the Stack has only one entry and one exit called the top.

Though a stack can be used for many computational problems, this article only focuses on its use for examining the nature of parentheses in a mathematical expression— In this case, using the JavaScript programming Language.

When the machine parses an expression containing parentheses, especially in typed languages, it first examines whether its parentheses are balanced before evaluating it. This algorithm takes the following steps:

  1. The expression is parsed as a string of mathematical symbols.

  2. The program loops through the expression from left to right, and pushes any opening bracket into a stack.

  3. The looping process verifies if there is a closing bracket and compares it with the element on top of the stack to determine if they match.

  4. The program pops the compared element from the stack if they match.

If, at the end of this process, the created stack is empty, then the expression contains balanced parentheses. If it’s not empty, the parentheses are unbalanced.

In JavaScript, this process is virtually the same, except for the difference in language syntax and style. The use of loops and control structures creates a logical framework for the program to analyze each symbol or token and make a decision. Here’s a JavaScript algorithm to check for balanced parentheses:

//Create a class for the stack with properties and methods.

class Stack {

constructor( ) {

this.dataStore = [ ];

this.top = 0;

}

push(element) {

this.dataStore[top++] = element;

}

pop( ) {

return this.dataStore[--top];

}

peek( ) {

return this.dataStore[top - 1];

}

clear( ) {

this.dataStore = [ ];

this.top = 0;

}

isEmpty( ) {

if (this.top === 0) {

return true;

};

}

}

After creating the Stack Class, you can create a stack object from the parent Class.

//function for parentheses checking.

function parChecker(exp) {

let num = exp.toString( );

const numStack = new Stack( );

for (let I = 0; I < num.length; i++) {

if (num[i] === “(“ || num[i] === “[“ || num[i] === “{“) {

numStack.push(num[i]);

}

if (num[i] === “)”) && numStack[top - 1] === “(“) {

numStack.pop( );

}

else {

numstack.push(num[i]);

}

if (num[i] === “}”) && numStack[top - 1] === “{“) {

numStack.pop( );

}

else {

numstack.push(num[i]);

}

if (num[i] === “]”) && numStack[top - 1] === “[“) {

numStack.pop( );

}

else {

numstack.push(num[i]);

}

}

if (numStack.isEmpty( )) {

console.log(“Espression has balanced parentheses”);

}

else {

console.log(“Expression has unbalanced parentheses”);

}

}

The Stack is one of many Data Structures in computer programming. It is used when order is of great importance to the task at hand. Think of a game where a bar contains 3 different colors arranged at random, but you have to fill it with the same color. To do this, you’ll have to do a lot of pops and pushes. That is exactly how the stack works to make our above program functional.

0
Subscribe to my newsletter

Read articles from Ugoaneni Victor directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ugoaneni Victor
Ugoaneni Victor

A final year Computer Science Student with strong interest in Computer Networks, Machine Learning, and Language Implementation.