Skip to content

Commit 39fff1b

Browse files
committed
updated code chapter 3
1 parent 0ec4da9 commit 39fff1b

7 files changed

+80
-39
lines changed

chapter03/01-Stack.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function Stack() {
22

3-
var items = [];
3+
let items = [];
44

55
this.push = function(element){
66
items.push(element);

chapter03/01-Stack2.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,33 @@ class Stack2 {
88

99
push(element){
1010
this[_items].push(element);
11-
};
11+
}
1212

1313
pop(){
1414
return this[_items].pop();
15-
};
15+
}
1616

1717
peek(){
1818
return this[_items][this[_items].length-1];
19-
};
19+
}
2020

2121
isEmpty(){
2222
return this[_items].length == 0;
23-
};
23+
}
2424

2525
size(){
2626
return this[_items].length;
27-
};
27+
}
2828

2929
clear(){
3030
this[_items] = [];
31-
};
31+
}
3232

3333
print(){
34-
console.log(this[_items].toString());
35-
};
34+
console.log(this.toString());
35+
}
3636

3737
toString(){
3838
return this[_items].toString();
39-
};
39+
}
4040
}

chapter03/01-Stack3.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,40 @@ let Stack3 = (function () {
1212
let s = items.get(this);
1313
s.push(element);
1414
items.set(this, s);
15-
};
15+
}
1616

1717
pop(){
1818
let s = items.get(this);
1919
let r = s.pop();
2020
items.set(this, s);
2121
return r;
22-
};
22+
}
2323

2424
peek(){
2525
let s = items.get(this);
2626
return s[s.length-1];
27-
};
27+
}
2828

2929
isEmpty(){
3030
return items.get(this).length == 0;
31-
};
31+
}
3232

3333
size(){
3434
let s = items.get(this);
3535
return s.length;
36-
};
36+
}
3737

3838
clear(){
3939
items.set(this, []);
40-
};
40+
}
4141

4242
print(){
43-
let s = items.get(this);
4443
console.log(this.toString());
45-
};
44+
}
4645

4746
toString(){
4847
return items.get(this).toString();
49-
};
48+
}
5049
}
5150

5251
return Stack3;

chapter03/01-StackES6.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Stack {
2+
3+
constructor () {
4+
this.items = [];
5+
}
6+
7+
push(element){
8+
this.items.push(element);
9+
}
10+
11+
pop(){
12+
return this.items.pop();
13+
}
14+
15+
peek(){
16+
return this.items[this.items.length-1];
17+
}
18+
19+
isEmpty(){
20+
return this.items.length == 0;
21+
}
22+
23+
size(){
24+
return this.items.length;
25+
}
26+
27+
clear(){
28+
this.items = [];
29+
}
30+
31+
print(){
32+
console.log(this.toString());
33+
}
34+
35+
toString(){
36+
return this.items.toString();
37+
}
38+
}

chapter03/02-UsingStacks.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var stack = new Stack();
1+
let stack = new Stack2();
22
console.log(stack.isEmpty()); //outputs true
33
stack.push(5);
44
stack.push(8);
@@ -15,10 +15,10 @@ var stack = new Stack();
1515

1616
//how to ensure true privacy
1717
//in case using Stack 2 uncomment code below
18-
/*var objectSymbols = Object.getOwnPropertySymbols(stack);
18+
let objectSymbols = Object.getOwnPropertySymbols(stack);
1919

20-
console.log(objectSymbols.length); // 2
21-
console.log(objectSymbols); // [Symbol(a), Symbol(b)]
22-
console.log(objectSymbols[0]); // Symbol(a)
20+
console.log(objectSymbols.length); // 1
21+
console.log(objectSymbols); // [Symbol()]
22+
console.log(objectSymbols[0]); // Symbol()
2323
stack[objectSymbols[0]].push(1);
24-
stack.print();*/
24+
stack.print(); //5, 8, 1

chapter03/03-BalancedSymbols.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
function matches(open, close){
2-
var opens = "([{",
3-
closers = ")]}";
4-
return opens.indexOf(open) == closers.indexOf(close);
5-
}
6-
71
function parenthesesChecker(symbols){
82

9-
var stack = new Stack(),
3+
let stack = new Stack(),
104
balanced = true,
115
index = 0,
12-
symbol, top;
6+
symbol, top,
7+
opens = "([{",
8+
closers = ")]}";
139

1410
while (index < symbols.length && balanced){
1511
symbol = symbols.charAt(index);
16-
if (symbol == '('|| symbol == '[' || symbol == '{'){
12+
if (opens.indexOf(symbol) >= 0){
1713
stack.push(symbol);
14+
console.log(`open symbol - stacking ${symbol}`);
1815
} else {
16+
console.log(`close symbol ${symbol}`);
1917
if (stack.isEmpty()){
2018
balanced = false;
19+
console.log('Stack is empty, no more symbols to pop and compare');
2120
} else {
2221
top = stack.pop();
23-
if (!matches(top, symbol)){
22+
//if (!matches(top, symbol)){
23+
if (!(opens.indexOf(top) === closers.indexOf(symbol))) {
2424
balanced = false;
25+
console.log(`poping symbol ${top} - is not a match compared to ${symbol}`);
26+
} else {
27+
console.log(`poping symbol ${top} - is is a match compared to ${symbol}`);
2528
}
2629
}
2730
}
@@ -33,5 +36,6 @@ function parenthesesChecker(symbols){
3336
return false;
3437
}
3538

36-
console.log(parenthesesChecker('{{([][])}()}'));
37-
console.log(parenthesesChecker('[{()]'));
39+
console.log(parenthesesChecker('{([])}')); //true
40+
console.log(parenthesesChecker('{{([][])}()}')); //true
41+
console.log(parenthesesChecker('[{()]')); //false

chapter03/05-TowerOfHanoi.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function towerOfHanoi(n, from, to, helper){
33
if (n > 0){
44
towerOfHanoi(n-1, from, helper, to);
55
to.push(from.pop());
6-
console.log('-----')
6+
console.log('-----');
77
console.log('Source: ' + from.toString());
88
console.log('Dest: ' + to.toString());
99
console.log('Helper: ' + helper.toString());
@@ -19,7 +19,7 @@ source.push(1);
1919
var dest = new Stack();
2020
var helper = new Stack();
2121

22-
towerOfHanoi(3, source, dest, helper);
22+
towerOfHanoi(source.size(), source, dest, helper);
2323

2424
source.print();
2525
helper.print();

0 commit comments

Comments
 (0)