-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalid_parenthesis.js
More file actions
33 lines (31 loc) · 2.18 KB
/
valid_parenthesis.js
File metadata and controls
33 lines (31 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/******************************************************************************************
* CODEWARS VALID PARENTHESIS CHALLENGE *
* *
* Problem Statement *
* Write a function called that takes a string of parentheses, and determines if the order*
* of the parentheses is valid. The function should return true if the string is valid, & *
* false if it's invalid. *
* *
* Examples *
* Input 1: () *
* Output 1: true *
* *
* Input 2: )(())) *
* Output 2: false *
* *
* Input 3: ( *
* Output 3: false *
* *
* Input 4: (())((()())()) *
* Output 4: true *
*****************************************************************************************/
function validParentheses(parens) {
let parensCount = 0;
for (let i = 0; i < parens.length; i++) {
if (parens[i] == "(") parensCount++;
else if (parens[i] == ")") parensCount--;
if (parensCount < 0) return false;
}
if (parensCount === 0) return true;
else return false;
}