FAQ: Iterators - The .findIndex() Method


#1

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

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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

I have a question about JavaScript.
In the code below, I noticed that the argument of the function uses the variable “animal”, as in “animal === ‘elephant’”
However, the array was declared with the function “animals” not “animal,” so how does the script “know” that animal is an item in the array “animals”
I have noticed with with a few other pieces of code, such as where an array is declared as “numbers” and later a function is passed the argument “num”. But “num” and “animal” were never declared as variables by the code. How does this work and what are the rules around it?

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

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


#3

The variable is defined in the loop.

for (let animal = 0; animal < animals.length; animal++) {

}

#4

Would anyone be able to explain why the ?true : false was at the end of the code?

return animal[0] === ‘l’ ? true : false;


#5

That is a control flow device known as a ternary expression. The term refers to three components.

condition ? true case : false case
//        ^           ^
//     ternary      colon
//     operator    separator