Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Checking the validity of parentheses in JavaScript
We are required to write a JavaScript function that takes in a string str containing just the characters −
'(', ')', '{', '}', '[' and ']'
Our function should determine if the input string is valid.
An input string is valid if −
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
For example −
"()" is a valid parenthesis
"()[]{}" is a valid parentheses
"(]" is an invalid parenthesis
Example
The code for this will be −
const str = "()[]{}";
const isValid = (str = '') => {
const map=new Map();
map.set('{','}');
map.set('(',')');
map.set('[',']');
const b=[];
for(let i=0;i
Output
And the output in the console will be −
true
false
Advertisements
