Typo in variable name [solved]

Hi! if this code is right:

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

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

so why isn’t this one?

const startWithS = animals.findIndex(animal => {
    return animal[0] === 's';
});

What I mean is: .findeIndex() method return index of first element in the array which evaluates to true. Why code works in first example (evaluates to true) but doesn’t in second one? Same comparison operator is used (===). And how should code look like if I didn’t want to use ternary operator but just an if statement? this one won’t work:

const startWithS = animals.findIndex(animal => {
  if (animal[0] === 's') {
    return animal;
  };
});

Thanks in advance!

array.findIndex() will return the index of the value that satisfies the test condition.

if (array.findIndex(element => element === 's')) === 0)

The method returns the index of the string element the starts with 's'. If it is zero, then we know that the string starts with ‘s’.

Your first example is correct, but the variable name is not well written.
The variable name is ‘startsWithS’ and does not ‘startWithS’.

Hey, thx a lot! It was one of these times that one starts to feel kinda schizofrenic :wink:

thx!