-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack-PolindromeExample.js
More file actions
56 lines (43 loc) · 1.19 KB
/
Stack-PolindromeExample.js
File metadata and controls
56 lines (43 loc) · 1.19 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var Stack = function(){
this.count = 0;
this.storage = [];
}
// Adds a value onto the end of the stack
Stack.prototype.push = function(value) {
this.storage[this.count++] = value;
}
// Removes and returns the value at the end of the stack
Stack.prototype.pop = function() {
// Check to see if the stack is empty
if (this.count === 0) {
return undefined;
}
var result = this.storage[--this.count];
delete this.storage[this.count];
return result;
}
// Returns the length of the stack
Stack.prototype.size = function() {
return this.count;
}
//Checks if the stack is empty or not
Stack.prototype.isEmpty = function(){
return this.count === 0;
}
///Polindrome example//
var letters = []; // this is a Stack
var word = "kabak";
var reverseWord = "";
//put letters of word into Stack
for (var i = 0; i < word.length; i++) {
letters.push(word[i])
}
//pop off the stack in reverse order
for (var i = 0; i < word.length; i++) {
reverseWord += letters.pop();
}
if(word === reverseWord){
console.log(word + " is a polindrome");
}else{
console.log(word + " is not a polindrome");
}