How to find index of first number in array

How would you find the index of first number in an array that has both strings and numbers?

Ex. var array = [ “dog”, “cat”, 1, 2, 3];

we cannot have different types of element in array because array is a collection of one type of element.But in array we can find the element starting with arr[o] and so on till the length of the array -1.Hope this helps

This is not true, elements in an array can be very diverse , there is no construction

You can use the indexOf method

i was confused a bit thanks for correcting me

with the indexof I don’t think you can find the answer that I’m looking for. for indexof you have to manually put in the element that I’m looking for.

Ex. array.indexOf(1).

I know how to do that already. What I want to do with this code is to identify the first existing number from the array without manually typing in the element like above and returning the index.

I mean the array could have like 999 strings and like 1 number. I need to locate the 1st number from such array and return its index.

I actually know what typeof is, but not sure how to apply it here. Should I be using a loop?

var array = [ “dog”, “cat”, 1, 2, 3];
array.findIndex(el=>el.typeof == “number”); // returns 2

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function . Otherwise, it returns -1, indicating that no element passed the test.

Thank you for your suggestion, I will take care of that.

Thank you for your help, I got my answer!

Assuming you can use the length property you can simply loop backwards and return the index that satisfy the condition, like:

for (let i = array.length -1; i <= 0; i--) {
  if is true return i;
}

Note this is a stub. You probably need more that that.

It was not a duplicate topic. The other question was a first index of a number and the answer I got used an array method. The question I asked today was for last index and the answer should not involve array method.

I didn’t use the loop to get my answer last time and also used an array method last time as well. For this problem I want to avoid using array method. Could you give me any hint?

I understand why it’s a related topic now. Sorry.