-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.js
54 lines (45 loc) · 1.07 KB
/
stack.js
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
function Stack(){
//Creating an array for our elements
let items = [];
//Creating a function to push elements to the top of our array items
function addStack(elements){
items.push(elements);
};
//creating a pop(delete) function to remove the topmost element in the array
function removeStack(){
items.pop();
};
// Return the topmost element in the array
function peek(){
//subtracting -1 from the length of an item returns the last element in the arrays
return items[items.length-1];
};
function isEmpty(){
// if(items.length == 0){
// return false;
// }
return items.length == 0;
};
function size(){
return items.length;
};
function clear(){
return items = [];
};
return {
addStack,
removeStack,
peek,
isEmpty,
size,
clear
}
}
const newStack = new Stack();
newStack.addStack('bob')
newStack.addStack('love')
newStack.addStack('hi')
console.log(newStack.size())//3
console.log(newStack.peek())// hi
console.log(newStack.isEmpty()) // false
console.log(newStack.removeStack()) // hi