-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChallenge1.js
More file actions
42 lines (26 loc) · 1.04 KB
/
Challenge1.js
File metadata and controls
42 lines (26 loc) · 1.04 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
34
35
36
37
38
39
40
41
42
function validBraces(braces) {
const stack = [];
const openBraces = '([{';
const closeBraces = ')]}';
const matchingBraces = {
')': '(',
']': '[',
'}': '{'
};
for (let char of braces) {
if (openBraces.includes(char)) {
stack.push(char);
}
else if (closeBraces.includes(char)) {
if (stack.length === 0 || stack.pop() !== matchingBraces[char]) {
return false;
}
}
}
return stack.length === 0;
}
console.log(validBraces("(){}[]")); // => True, all braces match correctly
console.log(validBraces("([{}])")); // => True, all braces match correctly
console.log(validBraces("(}")); // => False, mismatched brace
console.log(validBraces("[(])")); // => False, order of braces is incorrect
console.log(validBraces("[({})](]")); // => False, unmatched closing brace