FAQ: Iterators - The .findIndex() Method

This community-built FAQ covers the “The .findIndex() Method” exercise from the lesson “Iterators”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise The .findIndex() Method

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: Why can the code use animal instead of animals?

3 posts were split to a new topic: What does ? true : false mean?

5 posts were split to a new topic: What is a callback function?

4 posts were split to a new topic: Typo in variable name [solved]

8 posts were split to a new topic: How can I use charAt()?

3 posts were split to a new topic: Couldn’t I use the startsWith method?

2 posts were merged into an existing topic: What does ? true : false

3 posts were split to a new topic: What is num?

12 posts were split to a new topic: What does animal[0] do?

5 posts were merged into an existing topic: What does ? true : false mean?

How to print not the index, but the value of that index.

Just a quick question, guys.

This is my code and, according to CodeCademy, it’s running the way it should. However, I would like to print not the value 3, which is the index of the array element “seal”, but it’s content in the form of a string output. How can I do this? I tried using values(), valueOf and toString() methods, but I must be doing something wrong. Do you know what it is?

const animals = [‘hippo’, ‘tiger’, ‘lion’, ‘seal’, ‘cheetah’, ‘monkey’, ‘salamander’, ‘elephant’];

const foundAnimal = animals.findIndex( animal => animal === ‘elephant’);

const startsWithS = animals.findIndex( animalS => animalS.charAt(0) === ‘s’);

console.log(startsWithS);

you could write your own algorithm, if you want a built-in function, i think you need find:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

1 Like

You were right, stetim. Thank you.

Hi;
I done it with startsWith(‘s’);
but my question is why the console.log print is only 3, while the index 6 also start with ‘s’.
const animals = [‘hippo’, ‘tiger’, ‘lion’, ‘seal’, ‘cheetah’, ‘monkey’, ‘salamander’, ‘elephant’];
const startsWithS = animals.findIndex(ss =>{
return ss.startsWith(‘s’);
});
console.log(startsWithS); // 3

1 Like

The .findIndex method only returns an index of the first instance it finds, else -1 if no match found.

2 Likes

In this exercise, I now understand the difference between .indexOf() and .findIndex() methods. The first part of this exercise can also be done with .indexOf() method, but the second cannot be done with .indexOf() method.

I’m having some issues with this exercise.

I was able to complete it using the normal index animals[0], but I also wanted to try my hand at doing it with .atChar(0).

After some initial issues, I learned that .atChar() only work with strings and not arrays, so I wanted to test that first:

const animals = 'seal'; 
console.log(animals.atChar(0));

But I keep getting the following traceback:

console.log(animals.atChar(0));
                    ^

TypeError: animals.atChar is not a function
    at Object.<anonymous> 

I’m really confused here. Why does there need to be a function, why can’t it just be a .method? Also, I seem to remember that essentially, built-ins are just functions by another name?

This is the same error I get when I try to do things like:

const animals = ['hippo', 'tiger', 'lion', 'seal', 'cheetah', 'monkey', 'salamander', 'elephant'];

const startsWithS = animals.findIndex(animal => {
  return animal.char(0) === 's' ? true : false;
});

or a slightly different version of the same code:

const callback = arr => arr.charAt(0) === 's';
console.log(callback(animals))

Did you mean, String.charAt()?

Yes. My though was that by using animals.findIndex(...) I would iterate over the contents of the array, which would basically hand animal.atChar(0) the individual strings within the array.
To my understanding, it should work, so I can’t understand why I get a “not a function” error here when a), afaik methods are functions , and b) why there has to be a function here in the first place.

Edit: I think part of my problem here is that I have no strict understanding yet of what ISN’T a callback.