File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ # 0003 Stack ( L-I )
2+
3+ ## Problem
4+
5+ Create a class that has 3 method and 1 property.
6+
7+ - Push method add new element to the stack
8+ - Pop method remove last added element and return removed element
9+ - Peek return last added element
10+ - Size property return stack size
11+
12+ ## Solution
13+
14+ ``` javascript
15+ class Stack {
16+ constructor () {
17+ this .stack = []
18+ }
19+
20+ push (value ) {
21+ this .stack .push (value)
22+ }
23+
24+ pop () {
25+ return this .stack .pop ()
26+ }
27+
28+ peek () {
29+ return this .stack .at (- 1 )
30+ }
31+
32+ get size () {
33+ return this .stack .length
34+ }
35+ }
36+ ```
37+
38+ ## References
39+
40+ - [ Class MDN] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes )
41+ - [ Stack GeeksForGeeks] ( https://www.geeksforgeeks.org/implementation-stack-javascript/ )
42+ - [ Array MDN] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array )
43+
44+ ## Problem Added By
45+
46+ - [ GitHub] ( https://www.github.com/kennarddh )
47+
48+ ## Contributing
49+
50+ Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
51+
52+ Please make sure to update tests as appropriate.
You can’t perform that action at this time.
0 commit comments