File tree Expand file tree Collapse file tree 5 files changed +117
-0
lines changed Expand file tree Collapse file tree 5 files changed +117
-0
lines changed Original file line number Diff line number Diff line change 1+ class Queue {
2+ constructor ( ) {
3+ this . input = [ ] ;
4+ }
5+
6+ add ( element ) {
7+ this . input . push ( element ) ;
8+ }
9+
10+ remove ( ) {
11+ return this . input . shift ( ) ;
12+ }
13+ }
14+
15+ module . exports = Queue ;
Original file line number Diff line number Diff line change 1+ class Queue {
2+ constructor ( ) {
3+ this . input = [ ] ;
4+ this . output = [ ] ;
5+ }
6+
7+ add ( element ) {
8+ this . input . push ( element ) ;
9+ }
10+
11+ remove ( ) {
12+ if ( ! this . output . length ) {
13+ while ( this . input . length ) {
14+ this . output . push ( this . input . pop ( ) ) ;
15+ }
16+ }
17+ return this . output . pop ( ) ;
18+ }
19+ }
20+
21+ module . exports = Queue ;
Original file line number Diff line number Diff line change 1+ const Queue = require ( './queue-1.js' ) ;
2+
3+ describe ( 'Queue' , function ( ) {
4+ let queue ;
5+
6+ beforeEach ( ( ) => {
7+ queue = new Queue ( ) ;
8+ } ) ;
9+
10+ describe ( '#add' , ( ) => {
11+ it ( 'should push an element to the queue' , ( ) => {
12+ queue . add ( 1 ) ;
13+ expect ( queue . input ) . toEqual ( [ 1 ] ) ;
14+ } ) ;
15+ } ) ;
16+
17+ describe ( '#remove' , ( ) => {
18+ beforeEach ( ( ) => {
19+ queue . add ( 'a' ) ;
20+ queue . add ( 'b' ) ;
21+ } ) ;
22+
23+ it ( 'should get last element entered' , ( ) => {
24+ expect ( queue . remove ( ) ) . toEqual ( 'a' ) ;
25+ expect ( queue . remove ( ) ) . toEqual ( 'b' ) ;
26+ } ) ;
27+
28+ it ( 'should keep the order after some addition' , ( ) => {
29+ expect ( queue . remove ( ) ) . toEqual ( 'a' ) ;
30+ queue . add ( 'c' ) ;
31+ expect ( queue . remove ( ) ) . toEqual ( 'b' ) ;
32+ expect ( queue . remove ( ) ) . toEqual ( 'c' ) ;
33+ expect ( queue . remove ( ) ) . toBe ( undefined ) ;
34+ } )
35+ } ) ;
36+ } ) ;
Original file line number Diff line number Diff line change 1+ class Stack {
2+ constructor ( ) {
3+ this . input = [ ] ;
4+ }
5+
6+ push ( element ) {
7+ this . input . push ( element ) ;
8+ return this ;
9+ }
10+
11+ pop ( ) {
12+ return this . input . pop ( ) ;
13+ }
14+ }
15+
16+ module . exports = Stack ;
17+
Original file line number Diff line number Diff line change 1+ const Stack = require ( './stack.js' ) ;
2+
3+ describe ( 'Stack' , function ( ) {
4+ let stack ;
5+
6+ beforeEach ( ( ) => {
7+ stack = new Stack ( ) ;
8+ } ) ;
9+
10+ describe ( '#push' , ( ) => {
11+ it ( 'should push an element to the stack' , ( ) => {
12+ stack . push ( 1 ) ;
13+ expect ( stack . input ) . toEqual ( [ 1 ] ) ;
14+ } ) ;
15+ } ) ;
16+
17+ describe ( '#pop' , ( ) => {
18+ beforeEach ( ( ) => {
19+ stack . push ( 'a' ) ;
20+ stack . push ( 'b' ) ;
21+ } ) ;
22+
23+ it ( 'should get last element entered' , ( ) => {
24+ expect ( stack . pop ( ) ) . toEqual ( 'b' ) ;
25+ expect ( stack . pop ( ) ) . toEqual ( 'a' ) ;
26+ } ) ;
27+ } ) ;
28+ } ) ;
You can’t perform that action at this time.
0 commit comments