<script> var oilPrices = [70.15, 69.50, 71.23, 74.32, 69.50, 76.99]; var searchAtBeginning = oilPrices.lastIndexOf(60.50); var searchAtIndex = oilPrices.lastIndexOf(60.50,2); console.log("searchAtBeginning: " + searchAtBeginning); console.log("searchAtIndex: " + searchAtIndex); </script>
Here is the output:
As you can see the first lastIndexOf() method call returns the index of 4 because that's where 60.50 resides. It was able to find the element because the second argument was omitted therefore the search starts at the end of the array and returns the first index that it finds from the end. The second lastIndexOf() method call has the value of 1 as the second argument that tells the method start the search at index 2 in the array because that's the new end of the array according to the method.
Thanks I will use these tips!
ReplyDelete