Stack Practice
by Steven Senkus
JavaScript
function Stack() {
this.dataStore = [];
this.top = 0;
}
Stack.prototype.push = function (element) {
this.dataStore[this.top++] = element;
}
Stack.prototype.peek = function() {
return this.dataStore[this.top - 1];
}
Stack.prototype.pop = function () {
return this.dataStore[--this.top];
}
Stack.prototype.clear = function() {
this.top = 0;
}
Stack.prototype.length = function() {
return this.top;
}
function isPalindrome(word) {
var stack = new Stack();
for (var i = 0; i < word.length; i++) {
stack.push(word[i]);
}
var rword = '';
while (stack.length() > 0) {
rword += stack.pop();
}
if (rword === word) {
return true;
} else {
return false;
}
}
//console.log(isPalindrome('howdydwoh'));
//console.log(isPalindrome('fuof'));
function factorial(n) {
var s = new Stack();
while (n > 1) {
s.push(n--);
}
var product = 1;
while (s.length() > 0) {
product *= s.pop();
}
return product;
}
console.log(factorial(8))