forked from preeti-14-7/JavaScript-Program
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07problem.js
More file actions
34 lines (28 loc) · 676 Bytes
/
07problem.js
File metadata and controls
34 lines (28 loc) · 676 Bytes
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
34
/**
* Write function called areParenthesisValid that takes string
* and finds out if parenthesis in this string are valid
* */
function sameFrequency(str) {
let currentlyLeftOpen = 0;
for (let chr of str) {
switch (chr) {
case "(":
currentlyLeftOpen++;
break;
case ")":
currentlyLeftOpen--;
break;
}
if (currentlyLeftOpen < 0) return false;
}
return currentlyLeftOpen === 0;
}
function assert(thing) {
if (!thing) {
throw new Error("AssertionError");
}
}
// Examples:
assert(sameFrequency("((1+1))") === true);
assert(sameFrequency("(((ab)c)") === false);
assert(sameFrequency(")(") === false);