Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript, finding an array instance with indexOf() and lastIndexOf() methods

I'm reading "Professional JavaScript for Web Developers" (third edition) by Nicholas Zakas in an attempt to teach myself JS. However, I am having difficulty following the Location Methods section of chapter 5 on page 118 (in case you have the book). He explains that "the indexOf() method starts searching from the front of the array (item 0) and continues to the back, whereas lastIndexOf() starts from the last item in the array and continues to the front". Also he explains that "Each of these methods accepts two arguments: the item to look for and an optional index from which to start looking". He then attempts to illustrate this with examples.

As you can see below, to the right of the alert statements, he has listed what the correct output will be for each statement given the supplied argument(s). I do not understand how these outputs are determined. For example, how does alert(numbers.indexOf(4)); produce 3? I was reading this last night and thought I was just too tired to understand, however, I still cannot seem to figure out how this is achieved. I searched the Errata section from the book's companion website for a possible typo, but nothing was listed. I also searched elsewhere but found examples that mostly dealt with strings instead of numbers. Thanks for any help. This is my first post to stack overflow so my apologies if I have done something incorrect in my post.

His examples:

var numbers = [1,2,3,4,5,4,3,2,1];

alert(numbers.indexOf(4));        //3

alert(numbers.lastIndexOf(4));    //5

alert(numbers.indexOf(4, 4));     //5

alert(numbers.lastIndexOf(4, 4)); //3  

The way I thought the outcome would be:

alert(numbers.indexOf(4));        
//the item in the array with the fourth index, or 5


alert(numbers.lastIndexOf(4));    
//5 (this was only one that seemed to make sense to me) by counting back from the last value


alert(numbers.indexOf(4, 4));     
//start looking at index 4, or 5, and then count right four places to end up at 1 (last item in array).                                  


alert(numbers.lastIndexOf(4, 4)); 
//1, counting back to the left from the value with index 4, or 5, to reach the first value in the array.

Any help in determining the outputs based on the required argument and then how to also count from a specified value given the additional optional argument would be much appreciated. Thanks again.

like image 807
bonsigh Avatar asked Dec 26 '22 15:12

bonsigh


2 Answers

In most of the Programming languages, default indexing start from 0. Therefore, you have an understanding problem. Double consider your example with index starting from 0.

var numbers = [1,2,3,4,5,4,3,2,1];

alert(numbers.indexOf(4));        //3, because 4 is at 3rd index
alert(numbers.lastIndexOf(4));    //5, because last 4 is at 5th index
alert(numbers.indexOf(4, 4));     //5, because searching will start from 4th index
alert(numbers.lastIndexOf(4, 4)); //3, because searching will start from last 3rd element.  
like image 149
Parkash Kumar Avatar answered Dec 28 '22 07:12

Parkash Kumar


JavasScript arrays are zero indexed, in other words, the first item has an index of zero. This is true for almost all programming languages (apart fro XPath for some odd reason!).

The indexOf function returns the index of the first item it finds that equals the supplied argument.

var numbers = [1,2,3,4,5,4,3,2,1];

var index = numbers.indexOf(4);  // index is 3

alert(numbers[index]); // outputs 4
like image 45
ColinE Avatar answered Dec 28 '22 05:12

ColinE