FAQ: Iterators - The .findIndex() Method

I’m very new to coding and struggling a little to wrap my head around it.
I’ve tried different things with this exercise and I’m not understanding where it’s going wrong. Could someone help explain the code to me or point me to the right resources? Thank you

const startsWithS = animals => animals.findIndex(findS => { 

  return findS.charAt(0) === 's'


Did you pass in a function to .findIndex() that returns the index of the element with a first character of 's' ?: expected [Function

1 Like

startsWithS can just be a variable containing the result of calling findIndex on animals array. Why did you make startsWithS a function?

i think i have a problem here. i did something else and i was returned wrong so i checked the answer for the second one and this was
const startsWithS = animals.findIndex(animal => {

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

Is this code not checkin for whether the first string begins with ‘s’ or not???
Because according to the instructions the code shoul find amongst the list of arrays one that begins with ‘s’.

Not quite; it searches for the first string that begins with ‘s’, which might be anywhere in the string, or not at all.

I thought it needed to be a function.

and by it you mean startsWithS? Why did you think that? And have you resolved the issue now?

I’m not understanding your questions. I meant I thought the code as a whole needed to be completed with a function. I looked at other examples of .charAt and tried put it together in my example. I don’t really understand how to complete the exercise with that method.
I used another method and moved on from this exercise still unsure.

So then, by saying ‘… which might be anywhere in the string …’ , you mean the ‘s’ could be in the middle of the string word for example ‘constant’ and it still locates it

which might be anywhere in the string

Will need to go back into the lesson to get some context. Are we working with a long string (sentence) or an array of strings? If an array, then change string above, to array. The objective is to find words that begin with s if I’m not mistaken.

First up to the few mods I always see replying on these forums, thanks.

I decided to write the answers using different syntax as I normally use the syntax in a.

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

b. const startsWithS = animals.findIndex(animal => {
return animal [0] === ‘s’

I’m not too clear on the benefits of using the syntax in a vs b, or whether I’m going to run into trouble by carrying on writing the way I do in a, is there any documentation I can get stuck into as to why one would be better than the other or vice versa?


Can we refactor the code in example, a? Not really; it’s about as concise as it can be, and it is readable.

Can re refactor the code in example, b? Yes, but only slightly.

const startsWithS = animals.findIndex(animal => animal[0] === ‘s’);

Again, this is only a slight refactoring which makes it more concise.

Bottom line, the only real benefit of conciseness is readability and ease of debugging. A small added benefit is brevity and expressiveness.

Not sure where to find documentation relating to usage best practices, but it would be worth your while to dig around for articles and style guides that discuss when to and when not to use concise arrow function syntax.

Thanks mate, much appreciated. I’ve also searched for the answers on a ton of other things and usually your replies have covered it so thanks for those as well.

1 Like

Hi, a quick syntax question.

In the example:

const jumbledNums = [123, 25, 78, 5, 9]; 

const lessThanTen = jumbledNums.findIndex(num => {
  return num < 10;

{}'s are used when entering the function, but I completed the task using the syntax without the {}'s like this:

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

My question is, why didn’t I need the {}'s for the function/syntax to work? Also, which method is preferable?

Thanks in advance for the help.

PS. I’d also appreciate if someone could help me with what terminology i should’ve used instead of {}'s and “this and that syntax” in my question to describe my problem hahah DS.

Omitting the curly brackets and return keyword is a shorthand, but is only allowed when the body of your function is a single line.

Which is the case for your lessThenTen, so you could use the shorthand

1 Like

So i did this on the second exercise:

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

const startsWithS = animals.findIndex(sWord => {
if (sWord[0] === ‘s’){
return sWord;

console.log(startsWithS); // prints 3

But i checked the “solution code” and it appears to change the if statement for a “ternary operator” :

const startsWithS = animals.findIndex(animal => {
  return animal[0] === 's' ? true : false;

First sight is a better syntax , but my question since they teach us different methods and seems we can choose from what we feel comfortable with, is it really better to use ternary operator? having on mind that in a future we might more complex codes (working or developing)

Personally I would suggest that neither if nor a ternary are best suited for this. The comparison is already a boolean; just return it.

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

Seeing as we are already studying iterators one can assume we are versed in conditionals such as if and the ternary expression. When choosing which to use, ask first what the return value will be. Boolean? Return the expression. Some value or function? Possibly a ternary, but it should be simple and concise. If there is any verbosity then stick to if.

1 Like

Thank you very much Roy! helped a lot ! :slight_smile:

1 Like
const castTheNine = n => n > 9 ? n - 9 : n;

There we can justify the ternary as it is concise and pure with no confusion.

By what i understand on this function:

const castTheNine = n => n > 9 ? n - 9 : n;

Is that , it is a function with a parameter of (n), which actually determines than if n(value) is greater that number 9 it will be true… but then i get lost. Can you explain it please . what is the - symbol?
Thank you :slight_smile:

That is subtraction. If the number is greater than 9, subtract 9 from it and return, else return the number.

This is not a predicate function, but an alternate return value. The return is still a number. Only the expression before the ternary operator (?) is boolean. The first branch is on truthy, and the second is the default.