pedroyanky
pedroyanky

Reputation: 323

indexOf and lastIndexOf is giving same thing in an otherwise different situation

var arrays = ['stew', 'rice', 'Beans', 'yam', 'plantain', 'potatoe',
  'margarine', 'barbeque', 'semovota', 'bournvita'
];
var arr3 = arrays.lastIndexOf('stew');
console.log(arr3);

Both indexOf and lastIdexOf gives thesame thing 0 in both cases how so when lastIndexOf counts from the back. the correct answer seems like 9

Upvotes: 0

Views: 624

Answers (7)

Vansh Bhardwaj
Vansh Bhardwaj

Reputation: 446

lastIndexOf() doesn't count from the back, it simply gives you the index of the last duplicate item searching for. If you put 'stew' again in the array somewhere past the 0 index it will give you its index while indexOf will give the first index.

"indexOf" -> what is the position of the stew element at first occurance.

"lastIndexOf -> what is the position of the stew element at last occurrence.

if array would be like -> var arrays = ['stew', 'rice', 'Beans', 'yam', 'plantain', 'potatoe','margarine', 'barbeque', 'semovota', 'bournvita' , 'stew'];

arrays.lastIndexOf('stew') will return 10

Upvotes: 0

SleX
SleX

Reputation: 149

"indexOf" -> what is the position of the first element.

"lastIndexOf -> what is the last element.

If you want the position of the last from back to front see below:

var listagem = ['stew', 'rice', 'semovota', 'stew', 'bournvita'];
var postFirstIndex = listagem.indexOf('stew');
var postLastIndex = listagem.lastIndexOf('stew');
var postLastIndexInverted = listagem.length - listagem.lastIndexOf('stew') - 1;
alert(postFirstIndex);
alert(postLastIndex);
alert(postLastIndexInverted);

Upvotes: 1

Rupal
Rupal

Reputation: 1109

lastIndexOf gives you the the starting index but count last occurance inside the array from the right. In this there is single occurance so it is giving 0, but when you enter more than one occurance then you will see the difference:-

var arrays = ['stew','rice','Beans','yam','plantain','potatoe',
      'margarine','barbeque','semovota','stew','bournvita'];
       var arr3 = arrays.lastIndexOf('stew');
       console.log(arr3); // will give you 9
       var arr3 = arrays.lastIndexOf('margarine');
       console.log(arr3); // will give you 6 

now it give you 9. because stew comes 2 time

Upvotes: 0

Lee Brindley
Lee Brindley

Reputation: 6472

There is only 1 value "stew" In your array, positioned at index 0. That is the first and last index for "stew", thus that is why both indexOf and lastIndexOf are returning you 0.

Add a second "stew" value to your arrays variable, then observe the output from your logs.

Also lastIndexOf does not start counting from the last index, it'd make no difference what direction that function searched from, as it would have to cycle through N elements, where N is the length of the given array.

Example below

var source = ["foo", "bar", "foo"];

console.log(source.indexOf("foo"));
//Above will log 0.

console.log(source.lastIndexOf("foo"));
//Above will log 2

console.log(source.lastIndexOf("bar"));
//Above will log 1

console.log(source.lastIndexOf("bar") === source.indexOf("bar"));
//Above will log true, as "bar" occurs only once in the array,
//just like "stew" in your question.

console.log(source.indexOf("cheese"));
//Above will log -1, just for fun ha ha

Hope that helps.

Upvotes: 0

CodeAt30
CodeAt30

Reputation: 884

lastIndexOf() doesn't count from the back, it simply gives you the index of the last duplicate item searching for. If you put 'stew' again in the array somewhere past the 0 index it will give you its index while indexOf will give the first index.

example:

const array = ['stew','rice','Beans','yam','plantain', 'stew','potatoe','margarine','barbeque','semovota','bournvita'];

let regularIndex = array.indexOf('stew');
let lastIndex = array.lastIndexOf('stew');
console.log(regularIndex);
console.log(lastIndex);

// console prints:
// 0
// 5

Upvotes: 4

Coffee Monkey
Coffee Monkey

Reputation: 472

lastIndex does not start counting from the back. It returns the index starting from the beginning of the last matching item.

Upvotes: 3

Suresh Atta
Suresh Atta

Reputation: 121998

That is because either from starting or from backwards, "stew" is at same position which is 0.

From docs of lastIndexOf()

The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

You'll see the real difference when you have another "stew" in the middle or end.

var arrays = ['stew','rice','Beans','yam','plantain','potatoe',
      'margarine','barbeque','semovota','bournvita','stew'];
 var arr3 = arrays.lastIndexOf('stew');
 console.log(arr3); // 10

Upvotes: 6

Related Questions