indexOf and methods in general

So when I am trying to get a grasp on how some method works I will often end up
on es39?

For this case my question was:
‘Is indexOf basically looping through array?’

From what I’ve read I think ‘yes’. Correct me if I wrong please.

It needs to look at each element in the array from the start index until it finds a matching value. So yes, that’s a loop.

Lets say we have some function:
This one will return true if elem in arr, so theoretically we have chance, that we don’t need loop through all array?

function quickCheck(arr, elem) {
 
  if (arr.indexOf(elem) !== -1) {
    return true;
  }
  else {return false}
 
}

If I do something like this:

function quickCheck(arr, elem) {
 
  if (arr.indexOf(elem) == -1) {
    return false;
  }
  else {return true}
 
}

It will be worse implementation, because we are ALWAYS looping full length?

No, those two examples are functionally equivalent (they behave the same).

indexOf itself will stop looping and immediately return the index once it finds a match. Of course it has to loop through the whole array before it can return -1 when no match is found.

Why don’t you try implementing an indexOf function to help with your understanding? I guess it could have a signature like this:

function indexOf(array, searchValue) {
  // return the index of searchValue in the array
  // or -1 if searchValue is not found
}

expected behavior:

indexOf([1, 2, 3], 1) // 0
indexOf([1, 2, 3], 2) // 1
indexOf([1, 2, 3], 3) // 2
indexOf([1, 2, 3], 4) // -1
indexOf([1, 2, 3], "1") // -1
1 Like

Good stuff! Not right now, though.
Today is supposed to be CSS learning day(i was delaying it for a while :sweat_smile:)

Maybe a good thing to have in mind, there is also a lastIndexOf method, which will apparently return the last index of the element, starting to check the array from backwards. A simple way to check if an element is found more than once in an array, is to check indexOf and lastIndexOf and compare if they are different.
Another good place to look for more details, for example methods, is -

PS: Strings also share those two methods

1 Like

Thanks!
Already figured out about lastIndexOf and about strings.

This is cool trick, I bet it is often useful.

1 Like

Ended up with this.
Intentionally didn’t look up any stuff from es39.

function indexOf(array, searchValue) {
  /* TASK: return the index of searchValue in the array
   or -1 if searchValue is not found */
   
   //assumption: alternatives for iteration:
   // every or forEach methods
   
   //ideas for recursion: ~if we can do simple iteration> recursion fits
   //slice/splice array? need to look up palindrome impl.
  
  for (let i = 0; i < array.length; i++) {
    if (array[i] === searchValue) {  // == is no option here
      return i; //no need for break if quit func with return
    } 
  }
  return -1;
}

/* expected behavior
indexOf([1, 2, 3], 1) // 0
indexOf([1, 2, 3], 2) // 1
indexOf([1, 2, 3], 3) // 2
indexOf([1, 2, 3], 4) // -1
indexOf([1, 2, 3], "1") // -1
*/

/*tests environment onecompiler.com*/
console.log(indexOf([1, 2, 3], 1))//0
console.log(indexOf([1, 2, 3], 2))//1
console.log(indexOf([1, 2, 3], 3))//2
console.log(indexOf([1, 2, 3], 4))//-1
console.log(indexOf([1, 2, 3], "1"))//-1

/*thoughts
Worst case scenario: simple loop O(n)
Average case depends on input
*/