1

I am working on a textbook.

This is the question:

Define a function lastIndexOf, which, given an array and a value, returns the index of the last time the value occurs in the array. If the value never occurs, the function should return -1.

Then after try your function on the following:

console.log(lastIndexOf([ 0, 1, 4, 1, 2 ], 1), "=?", 3);

I know there is a lastindexof() method. I just don't know how to implement it in this function.

My question is, how should I tackle this?

I am a novice student, but how would you, with your experience programming, think about doing this? What would be your thought process? What should I know?

billygreen
  • 223
  • 3
  • 12

5 Answers5

1

Just go from the last element and return if you find what you are looking for.

Last index would be array.length - 1. Use classic for loop.

Good luck in your study!

Nondv
  • 769
  • 6
  • 11
1
  1. Start with index at the last element (length-1).
  2. If index < 0, return -1
  3. Compare the element at index with the 'needle'
  4. If match, return the index
  5. Decrement the index and repeat from #2

I would give you a snippet, but that would make it too easy :)

Steven Spungin
  • 27,002
  • 5
  • 88
  • 78
1

There are many ways to achieve it.

All depends on Your "creativity".


I'll write 3 of them:

1) Straight looping until last match:

const lastIndexOf = (haystack, needle) => {
  let index = -1;
  haystack.forEach(function(element, i) {
    if (element === needle) index = i;
  });
  return index;
}


let fruits = ['apple', 'mango', 'pear', 'strawberry', 'bananas', 'mango', 'cherry']

console.log('Index of:', fruits.indexOf('mango')); 
console.log('Last Index of:', lastIndexOf(fruits, 'mango'));
console.log('Last Index of:', lastIndexOf(fruits, 'potato'));

console.log(lastIndexOf([ 0, 1, 4, 1, 2 ], 1), "=?", 3);

2) Looping using -1 step and stopping at first match:

const lastIndexOf = (haystack, needle) => {
  for (let i = haystack.length -1; i >= 0; i--) {
    if (haystack[i] === needle) return i;
  }
  return -1;
}


let fruits = ['apple', 'mango', 'pear', 'strawberry', 'bananas', 'mango', 'cherry']

console.log('Index of:', fruits.indexOf('mango')); 
console.log('Last Index of:', lastIndexOf(fruits, 'mango'));
console.log('Last Index of:', lastIndexOf(fruits, 'potato'));

console.log(lastIndexOf([ 0, 1, 4, 1, 2 ], 1), "=?", 3);

3) Reverse sorting + "length math":

const lastIndexOf = (haystack, needle) => {
  const rIndex = haystack.reverse().indexOf(needle);
  return (rIndex > -1) ? haystack.length - rIndex - 1 : -1;
}


let fruits = ['apple', 'mango', 'pear', 'strawberry', 'bananas', 'mango', 'cherry']

console.log('Index of:', fruits.indexOf('mango')); 
console.log('Last Index of:', lastIndexOf(fruits, 'mango'));
console.log('Last Index of:', lastIndexOf(fruits, 'potato'));

console.log(lastIndexOf([ 0, 1, 4, 1, 2 ], 1), "=?", 3);


P.S. In case of very big arrays these 3 methods can be less optimal, since You cannot predict the value You're looking for is near to end or beginning of array.

So for such cases You can inspire from binary tree algorithm.

Everything depends on complexity of task.

num8er
  • 18,604
  • 3
  • 43
  • 57
1

Array.prototype.lastIndexOf() works like this:

var arr = [ 0, 1, 4, 1, 2 ];

console.log(arr.lastIndexOf(1));//<-- we look for 1
console.log(arr.lastIndexOf(5));//<-- we look for 5
//or
console.log([ 0, 1, 4, 1, 2 ].lastIndexOf(1));
Emeeus
  • 5,072
  • 2
  • 25
  • 37
1

There is already a function which does that called:

lastIndexOf()

But here is how it can be implemented yourself:

function lastIndex(arr, value) {
  let index = -1;

  for(let i=0; i < arr.length; i++) {
    if(arr[i] === value) {
      index = i;
    }
  }
  return index;
}

console.log(lastIndex([1,2,3,3,3,4], 3))
Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155