Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fcf3db6

Browse files
fix(linkedlist): rename indexOf to getIndex to make it clearer
1 parent 7659ac6 commit fcf3db6

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

‎src/data-structures/linked-lists/linked-list.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff 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

‎src/data-structures/linked-lists/linked-list.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /