-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
79 lines (65 loc) · 1.97 KB
/
stack.js
File metadata and controls
79 lines (65 loc) · 1.97 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// function for stack opeerations such as push, pop, length.
// stack opereration using javascript
let stackLetter = [] //this is our stack, declared as an array.
let word = 'madam' //this is our word to check if it is palindrome.
let word1 = 'checkforpalindrome'
let rword = [] //store the reverse word.
// pushing each letter of word into the stack.
for (let i = 0; i < word1.length; i++) {
stackLetter.push(word1[i])
}
// pop of each letter from stack one after the another.
for (let i = 0; i < word1.length; i++) {
rword += stackLetter.pop();
}
// check if the word and reverse of the word matches.
if(word == rword){
console.log(`${word1} is palindrome`)
}
else{
console.log(`${word1} is not a palindrome`)
}
// basic stack functioning using javascript
let stack = function(){
this.count = 0;
this.store ={};
// add the value in stack
this.push = function(value){
this.store[this.count] = value;
this.count++;
}
// get the value from the top of stack and reduces the stack.
this.pop = function(){
// if there is no value return undefined.
if(this.count === 0){
return undefined;
}
else{
this.count--; // first decrement the counter.
let result = this.store[this.count];
delete this.store[this.count];
return result;
}
}
// retunr the size os stack.
this.size = function(){
return this.count;
}
// return the value at the top of stack but doesent reduces the stack.
this.peek = function(){
return this.store[this.count-1]
}
}
// create a variable for stack operations.
let myStack = new stack();
// perform the stack operations.
myStack.push(1);
myStack.push(2);
console.log(myStack.peek());
console.log(myStack.pop());
console.log(myStack.peek());
myStack.push("freeCodeCamp");
console.log(myStack.size());
console.log(myStack.peek());
console.log(myStack.pop());
console.log(myStack.peek());