Skip to content

Commit 3d523ad

Browse files
author
Carlos Green
committed
stacks
1 parent 287a1f3 commit 3d523ad

File tree

6 files changed

+88
-13
lines changed

6 files changed

+88
-13
lines changed

Chapter4Stacks/fact.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const Stack = require("./stack.js")
2+
3+
function fact(n) {
4+
var s = new Stack()
5+
while(n > 1) {
6+
s.push(n--)
7+
}
8+
var product = 1
9+
while(s.length() > 0) {
10+
product *= s.pop()
11+
}
12+
return product
13+
}
14+
15+
console.log(fact(5), 5)
16+
console.log(fact(20), 20)

Chapter4Stacks/factorial.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function factorial(n) {
2+
if (n === 0) {
3+
return 1
4+
} else {
5+
return n * factorial(n - 1)
6+
}
7+
}
8+
9+
// 5! = 5 * 4 * 3 * 2 * 1 = 120
10+
console.log(factorial(5), 5)
11+
console.log(factorial(20), 20)

Chapter4Stacks/isPalindrome.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const Stack = require('./stack.js')
2+
3+
function isPalindrome(word) {
4+
var s = new Stack()
5+
for (var i = 0; i < word.length; ++i) {
6+
s.push(word[i])
7+
}
8+
var rword = "";
9+
while(s.length() > 0) {
10+
rword += s.pop()
11+
}
12+
if (word == rword) {
13+
return true
14+
} else {
15+
return false
16+
}
17+
}
18+
19+
var word = "hello"
20+
21+
if (isPalindrome(word)) {
22+
console.log(word + " is a palindrome.")
23+
} else {
24+
console.log(word + " is not a palindrome.")
25+
}
26+
27+
var word = "racecar"
28+
29+
if (isPalindrome(word)) {
30+
console.log(word + " is a palindrome.")
31+
} else {
32+
console.log(word + " is not a palindrome.")
33+
}

Chapter4Stacks/multiBase.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const Stack = require('./stack.js');
2+
3+
function multiBase(num, base) {
4+
var s = new Stack();
5+
do {
6+
s.push(num % base);
7+
num = Math.floor((num /= base));
8+
} while (num > 0);
9+
10+
var converted = '';
11+
while (s.length() > 0) {
12+
converted += s.pop();
13+
}
14+
return converted;
15+
}
16+
17+
var num = 32
18+
var base = 2
19+
var newNum = multiBase(num, base)
20+
21+
console.log(num + " converted to base " + base + " is " + newNum)
22+
23+
num = 125
24+
base = 8
25+
26+
var newNum = multiBase(num, base)
27+
console.log(num + " converted to base " + base + " is " + newNum)

Chapter4Stacks/stack.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,7 @@ function Stack() {
1818
this.dataStore.pop()
1919
}
2020
this.top = 0
21-
console.log(this.dataStore, 'data')
2221
}
2322
}
2423

25-
let st1 = new Stack()
26-
27-
st1.push('nice')
28-
st1.push('cool')
29-
st1.push('sweet')
30-
st1.push('nice')
31-
32-
console.log(st1.dataStore, 'store')
33-
st1.clear()
34-
console.log(st1.dataStore, 'store')
35-
st1.push('nice')
36-
console.log(st1.dataStore, 'store')
24+
module.exports = Stack

stack.js

Whitespace-only changes.

0 commit comments

Comments
 (0)