File tree Expand file tree Collapse file tree 8 files changed +86
-9
lines changed Expand file tree Collapse file tree 8 files changed +86
-9
lines changed Original file line number Diff line number Diff line change @@ -5,5 +5,5 @@ var jrunner = new Jasmine();
55// jrunner.configureDefaultReporter({print: noop}); // jasmine < 2.4.1, remove default reporter logs
66jrunner . env . clearReporters ( ) ; // jasmine >= 2.5.2, remove default reporter logs
77jrunner . addReporter ( new SpecReporter ( ) ) ; // add jasmine-spec-reporter
8- jrunner . loadConfigFile ( ) ; // load jasmine.json configuration
8+ jrunner . loadConfigFile ( 'jasmine.json' ) ; // load jasmine.json configuration
99jrunner . execute ( ) ;
File renamed without changes.
Original file line number Diff line number Diff line change 1515 "mocha" : " 3.2.0"
1616 },
1717 "scripts" : {
18- "test" : " node jasmine-runner.js"
18+ "test" : " jasmine JASMINE_CONFIG_PATH=jasmine.json # node jasmine-runner.js"
1919 },
2020 "keywords" : [],
2121 "author" : " " ,
Original file line number Diff line number Diff line change 11const Node = require ( './node' ) ;
2-
2+ /**
3+ * Singly LinkedList data structure
4+ */
35class LinkedList {
46 constructor ( ) {
57 this . head = null ;
68 this . tail = null ;
9+ this . length = 0 ;
710 }
811
912 /**
@@ -19,6 +22,7 @@ class LinkedList {
1922 this . head = node ;
2023 }
2124 this . tail = node ;
25+ this . length ++ ;
2226 return node ;
2327 }
2428
@@ -34,6 +38,7 @@ class LinkedList {
3438 this . tail = node ;
3539 }
3640 this . head = node ;
41+ this . length ++ ;
3742 return node ;
3843 }
3944
@@ -71,6 +76,10 @@ class LinkedList {
7176 }
7277 return data . join ( ' -> ' ) ;
7378 }
79+
80+ size ( ) {
81+ return this . length ;
82+ }
7483}
7584
7685function test ( ) {
Original file line number Diff line number Diff line change 1- const assert = require ( 'assert' ) ;
21const LinkedList = require ( './linkedlist' ) ;
32
43describe ( 'LinkedList' , function ( ) {
4+
55 describe ( '.addLast' , function ( ) {
6- it ( 'should add elements to the tail' , function ( ) {
6+
7+ it ( 'should add elements to the tail and count size' , function ( ) {
78 const list = new LinkedList ( ) ;
8- list . add ( 1 ) ;
9- list . add ( 2 ) ;
10- list . add ( 3 ) ;
11- list . add ( 4 ) ;
9+ list . addLast ( 1 ) ;
10+ list . addLast ( 2 ) ;
11+ list . addLast ( 3 ) ;
12+ list . addLast ( 4 ) ;
1213
1314 expect ( list . toString ( ) ) . toEqual ( '1 -> 2 -> 3 -> 4' ) ;
15+ expect ( list . size ( ) ) . toBe ( 4 ) ;
1416 } ) ;
17+
18+ it ( 'should add elements to the head and count size' , function ( ) {
19+ const list = new LinkedList ( ) ;
20+ list . addFirst ( 1 ) ;
21+ list . addFirst ( 2 ) ;
22+ list . addFirst ( 3 ) ;
23+ list . addFirst ( 4 ) ;
24+
25+ expect ( list . toString ( ) ) . toEqual ( '4 -> 3 -> 2 -> 1' ) ;
26+ expect ( list . size ( ) ) . toBe ( 4 ) ;
27+ } )
1528 } ) ;
1629} ) ;
Original file line number Diff line number Diff line change 1+ const LinkedList = require ( './linkedlist' ) ;
2+
3+ LinkedList . prototype . isPalindrome = function ( ) {
4+ const queue = [ ] ;
5+ let i1 = this . head ;
6+ let i2 = this . head ;
7+
8+ if ( this . size ( ) === 1 ) {
9+ return true ;
10+ }
11+
12+ queue . push ( i1 . data ) ;
13+
14+ // collect element up to the middle
15+ while ( i2 . next . next ) {
16+ i1 = i1 . next ;
17+ i2 = i2 . next . next ;
18+ if ( i2 . next ) { queue . push ( i1 . data ) ; }
19+ }
20+
21+ // compare they are the same as the second half
22+ while ( i1 . next ) {
23+ i1 = i1 . next ;
24+ if ( queue . pop ( ) !== i1 . data ) {
25+ return false ;
26+ }
27+ }
28+
29+ return true ;
30+ } ;
31+
32+ module . exports = LinkedList ;
Original file line number Diff line number Diff line change 1+ const LinkedList = require ( './palindrome' ) ;
2+
3+ describe ( 'LinkedList: isPalindrome' , function ( ) {
4+ let list ;
5+
6+ beforeEach ( function ( ) {
7+ list = new LinkedList ( ) ;
8+ } ) ;
9+
10+ it ( 'should pass' , function ( ) {
11+ list . addLast ( 'a' ) ;
12+ expect ( list . isPalindrome ( ) ) . toBe ( true ) ;
13+ } ) ;
14+ } ) ;
Original file line number Diff line number Diff line change 1+ {
2+ "files" : [
3+ " src/**/*.js"
4+ ],
5+
6+ "tests" : [
7+ " src/**/*[sS]pec.js"
8+ ]
9+ }
You can’t perform that action at this time.
0 commit comments