File tree Expand file tree Collapse file tree 2 files changed +11
-8
lines changed
src/data-structures/linked-lists Expand file tree Collapse file tree 2 files changed +11
-8
lines changed Original file line number Diff line number Diff line change @@ -101,14 +101,17 @@ class LinkedList {
101101 /**
102102 * Search by value. It finds first occurrence of
103103 * the position of element matching the value.
104+ * Similar to Array.indexOf.
105+ *
104106 * Runtime: O(n)
107+ *
105108 * @example : assuming a linked list with: a -> b -> c
106- * linkedList.indexOf ('b') // ↪️ 1
107- * linkedList.indexOf ('z') // ↪️ undefined
109+ * linkedList.getIndexByValue ('b') // ↪️ 1
110+ * linkedList.getIndexByValue ('z') // ↪️ undefined
108111 * @param {any } value
109112 * @returns {number } return index or undefined
110113 */
111- indexOf ( value ) {
114+ getIndexByValue ( value ) {
112115 return this . find ( ( current , position ) => {
113116 if ( current . value === value ) {
114117 return position ;
@@ -142,7 +145,7 @@ class LinkedList {
142145 // tag::find[]
143146 /**
144147 * Iterate through the list until callback returns a truthy value
145- * @example see #get and #indexOf
148+ * @example see #get and #getIndexByValue
146149 * @param {Function } callback evaluates current node and index.
147150 * If any value other than undefined it's returned it will stop the search.
148151 * @returns {any } callbacks's return value or undefined
Original file line number Diff line number Diff line change @@ -122,14 +122,14 @@ describe('LinkedList Test', () => {
122122 } ) ;
123123 } ) ;
124124
125- describe ( '#indexOf ' , ( ) => {
125+ describe ( '#getIndexByValue ' , ( ) => {
126126 it ( 'should find element index' , ( ) => {
127- expect ( linkedList . indexOf ( 0 ) ) . toBe ( 0 ) ;
128- expect ( linkedList . indexOf ( 'found' ) ) . toBe ( 1 ) ;
127+ expect ( linkedList . getIndexByValue ( 0 ) ) . toBe ( 0 ) ;
128+ expect ( linkedList . getIndexByValue ( 'found' ) ) . toBe ( 1 ) ;
129129 } ) ;
130130
131131 it ( 'should return undefined' , ( ) => {
132- expect ( linkedList . indexOf ( 'hola' ) ) . toBe ( undefined ) ;
132+ expect ( linkedList . getIndexByValue ( 'hola' ) ) . toBe ( undefined ) ;
133133 } ) ;
134134 } ) ;
135135
You can’t perform that action at this time.
0 commit comments