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.

@indice234 There is something called typeof you should read about.

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

You can use a while or for loop or an array method like findIndex along with typeof.

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.

@tango025 Instead of just posting the solution after I gave a hint, it would be more considerate and more beneficial to the OP if you stick with giving hints. Anyone can post a solution, but that does not teach anything (which is what we are all about here).

Thank you.

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

In the future, just stick to hints and not solutions (even blurred ones), especially if someone has already pointed the user in a direction with a hint.

1 Like

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.

Sorry about that, but honestly you would just reverse the loop iteration as you did to solve the other problem. That is why it really is a very related topic.

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.

The user above gave you the logic regarding a loop. If you are struggling writing loops, you might want to take a look at the Free Code Camp curriculum. There are many challenges dealing with iterating through arrays.