What does animal[0] do?

Hi,
In the code why animal[0] ?
in the previous codes they only mentioned animal or some other parameters but they did not use [0] . why [0] is used here?

the instructions:

Let’s see if we can find the index of the first animal that starts with the letter 's'

to get the first character of a string, we need to access by index. Which [0] does

2 Likes

animal[0]
this is not clear. in an array, [0] will return the first element and not the first character of each element.

the requirement to return the index of the element that starts with “s” should be a string manipulation function (like substr (size_t pos = 0, size_t len = npos) ).

so home come animal[0] is the solution??

5 Likes

It isn’t being used on an array.

Given a string, look at its first character, compare to s, determine whether the string meets the criteria

Repeat for each string.

2 Likes

what do you mean not an array??

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

that’s not an array declaration?

That’s an array. And that isn’t the value that is being looked at. It’s one of the strings in it that is being looked at.

'hippo'[0]

1 Like

exactly!
so animals[0] is NOT looking inside each element of an array but on the first element only.
to look at each element and find the first character of each, there should be a string operator like substr(). plus looping.

correct me if i’m wrong in my assumptions. really confused.

2 Likes

But that’s not what’s in the code. This is not in the code anywhere:
animals[0]

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

that’s the answer from the tutorial. see, how can animal[0] look at each element’s first character??

It doesn’t.
You’re mixing up a single animal with an array of animals. That function does not look at the array at all. It does not mention the array. It doesn’t do any operations involving the array.

1 Like

it is comparing the array animal so it’s doing operation involving the array.

let me quote the explanation of .findIndex()

Calling .findIndex() on an array will return the index of the first element that evaluates to true in the callback function.

how does it move within the array’s element then? to look at each element’s first character?

The callback function doesn’t move within the array, it is completely unaware of the array.
It looks at one string.
Array.findIndex does the rest.

Array.findIndex calls the callback function on each element of the array until the callback returns true or the end of the array is reached.

I’m confused. So callbackFunctionParameter[0] === 's'runs through the first letter of each string in an array?

1 Like

yes, given findIndex will pass each element in the array to the parameter of your callback function

3 Likes

OK, this seems to still be unanswered and even though it’s been half a year (or so) i’ll give it a go, maybe it will help someone else who might be confused.

Answer:

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

Lets break it down.

const startsWithS = animals.findIndex

declares and assigns startsWithS to whatever index it “finds” on the array animals[]

Now the part that seems to be confusing bad.banana and maybe others

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

inside of array animals[] lets declare a new variable animal, but really we could have called it x:

animals.findIndex(`x`=> {
  return `x`[0] === 's' ? true : false;

Then after we have the current index that is being look at by findIndex() method, lets look at x[0], or the first character of string x, if that value === “s”? return true : else return false.

I think the confusion was just the naming of the variable.

Hoping this clears it up, it might be a bit late for bad.banana but hope it clears things out for other people.

5 Likes

Here’s the secret :shushing_face:: The String object holds an array of characters to be a string!

(Strings are array-like; they are not an actual array)

Here’s the proof:

console.log(new String("Hello"));

outputs:

String {"Hello"}
0: "H"
1: "e"
2: "l"
3: "l"
4: "o"
length: 5
__proto__: String
[[PrimitiveValue]]: "Hello"

Thus, you can do "Hello"[0].

Arrays use bracket notation and numerical indexing to find a certain element in the array. Arrays are objects, and objects use similar notation (except it’s stored as keys and not numbers).

Strings aren’t arrays, but they are array-like.

This means that they have a .length.

Strings can also use the numerical notation to find a character at a certain index "Hello world"[0] returns "H".

In this case, you are looping through an array and trying to find the first character of each string and check if it begins with s (tbh case sensitivity is real and this:

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

should actually look like this:

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

, but that’s even more confusing so never mind). It’s kind of easy, except you are mixing up the animals array with finding the first character of each element in the array.

Simply speaking, you are holding an array of strings and you are checking the first character of each string to try to find out if that element begins with s.

1 Like

animals is an array, however the iterator function being called on the array is for each individual element in the array. Like .forEach(), the .findIndex() iterator is separating and evaluating each element of the array, so when each animal in the array is being evaluated at its [0] position, that would be the first letter in the string, not the first element in the array. Hope that’s helpful!

2 Likes

Repeated for each string UNTIL if finds the first string containing that letter. Then it stops.

Thanks for this. This is the simplest explanation I can understand given the tons of Explanation here.