-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathstack.js
More file actions
40 lines (30 loc) · 779 Bytes
/
stack.js
File metadata and controls
40 lines (30 loc) · 779 Bytes
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
const push = (stack, item) => [item, ...stack];
const pop = (stack) => stack.slice(1);
const top = (stack) => stack[0];
const isEmpty = (stack) => size(stack) === 0;
const size = (stack) => stack.length;
// testing
let stack = [];
console.log(isEmpty(stack));
stack = push(stack, 1);
console.log(stack);
stack = push(stack, 2);
console.log(stack);
stack = push(stack, 3);
console.log(stack);
stack = push(stack, 4);
console.log(stack);
stack = push(stack, 5);
console.log(stack);
console.log(top(stack));
console.log(size(stack));
console.log(isEmpty(stack));
stack = pop(stack);
stack = pop(stack);
stack = pop(stack);
stack = pop(stack);
console.log(isEmpty(stack));
console.log(top(stack));
stack = pop(stack);
console.log(isEmpty(stack));
console.log(size(stack));