-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_exercise.js
64 lines (57 loc) · 1.41 KB
/
stack_exercise.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//Used typically to store temporary data and when a LIFO nature is needed
// [()
// // ()]
// ([))
function LintBraces(somePhrase)
{
let bracesList = [];
let bracesStack = [];
let braces =
{
'{' : '}',
'[' : ']',
'(' : ')'
}
for(let i = 0; i < somePhrase.length; i++)
{
if(somePhrase[i] === '{' || somePhrase[i] === '(' || somePhrase[i] === '[' || somePhrase[i] === ']' || somePhrase[i] === ')' || somePhrase[i] === '}')
{
bracesList.push(somePhrase[i]);
}
}
for(let item of bracesList)
{
if(item === '{' || item === '(' || item === '[')
{
bracesStack.push(item);
}
else if(item === '}' || item === ')' || item === ']')
{
let toInspect = item;
let itemPoped = bracesStack[bracesStack.length - 1];
if(bracesStack.length !== 0)
{
bracesStack.pop();
}
else
{
return 'Error type 2';
}
if(toInspect !== braces[itemPoped])
{
return 'Error type 3';
}
//successfully paired so far
}
}
if(bracesStack.length !== 0)
{
return 'Error type 1';
}
return 'String braces are correctly paired';
}
console.log(LintBraces('(var x = {y: [1, 2, 3]})'));
console.log(LintBraces('(var x = {y: [1, 2, 3]}'));
console.log(LintBraces('var x = {y: [1, 2, 3]})'));
console.log(LintBraces('(var x = {y: [1, 2, 3]}}'));
//ctrl + s deletes the content of a line and goes to insert mode