FAQ: Iterators - The .findIndex() Method

so, my code for step 2 looks like this:

const startsWithS = animals.findIndex(animal => {
  return animal[0] === 's'

and when i click to show me the solution, the exercise shows me that i have to add a ? true : false . i know what that means, but why do i need to put that? or it’s just the plataform?

Given a comparison already result in a boolean (a comparison is either true or false), makes a ternary operator redundant.

1 Like

It may be that the author wants to explicitly demonstrate the ternary expression as having a true branch and a false branch (the default).

The code you have still returns one of true or false.

Since we’re using an arrow function, why not take advantage of the relaxed syntax?

const startsWithS = animals.findIndex(animal => animal[0] === 's');
1 Like

Hi Stetim94,

I did use .find but that only printed one element and not all that begin with letter s. I wanted to print all words that began with letter s.
The below code made sense after some trial and error.
const findObj = animals.filter(animal => animal.charAt(‘0’)=== ‘s’);

for(let i =0; i < findObj.length; i++){

console.log(findObj[i]);

}
Is the above code efficient if there a large array and can we combine the for within the arrow function to print a better output?

Thanks,
Adi

But whoever wrote that question that i answered, only wanted the first match. So then .find() is a good fit.

Using a function will give extra overhead, but then we are talking micro optimization.

const startsWithS = animals.findIndex( wordS => {

return wordS.charAt(0) === ‘s’;

})

console.log(startsWithS);

Why would you make the name of your parameter plural? Or uppercase?

Can I use the char.At() on a number? For example I want to find the first number that begins with a 5 from an array of numbers.

const numbers = [1, 200, 45, 566, 0, 564];

const startsWithFive = numbers.findIndex(num => {
return num.charAt(0) === 5;
}); /* <------- When do we add the semicolons? */

If not, what would be the best way of doing this? Also when do we add the semicolons at the end of a code block? I have seen some code blocks have them and others don’t.

Hi! I have a question regarding this exercise.
So, this is the code:

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

const foundAnimal = animals.findIndex( animal => {

  return animal === 'elephant';

});

const startsWithS = animals.findIndex ( animal => {

  return animal.charAt(0) === 's';

});

console.log(foundAnimal);

console.log(animals[7]);

console.log(startsWithS);

console.log(animals[3]);

My question is, why isn’t the index of ‘salamander’ is added in startsWithS variable?

The documentation says:

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function

first is the crucial word here. Seal is obviously the first match, so you get 3 (the index of seal)

In a prior optional project or exercise in this course, we had to use the .indexOf() method, which returns the index of a given element.
In this one we are taught .findIndex(), which does the same thing, but with a callback function as a parameter, which makes it more roundabout to achieve, seemingly, the same thing.

Is there any good example for when .findIndex() is preferable over .indexOf()?

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

const foundAnimal = animals.findIndex(animal => animal === 'elephant'); //returns 7

const findAnimal = animals.indexOf('elephant'); //also returns 7

They’re not really comparable since they are not looking for the same thing. .indexOf() is looking for primitives directly in the iterable. There is no callback function involved. The other iterator takes a callback function that allows it to deal with data structures by way of introduced logic. This takes it to another layer.

1 Like