Array Methods - Help please!


#1

I need help please.
Let’s find the index of a particular element in groceryList using .indexOf() .

Call .indexOf() on groceryList to find the index of the element 'pasta' and save the returned value to a const variable named pastaIndex .

Then log pastaIndex to the console. (You may remove the other console.log() statements to declutter the terminal.)


#2

The simplistic syntax with functions in JavaScript is this:

object.function(arguments);

It can be used with other functions and variables. Here’s an example:

const aVariable = object.function(argument1, argument2);

Adding arguments depend on the definition of a function.

So if we apply this with the step you’re struggling with, it should be this:

const pastaIndex = groceryList.indexOf('pasta');

Let’s also not forget to log the variable we saved it to, as it is instructed.

console.log(pastaIndex);

#3

When they are attributes of an object, they are methods.


#4

Well technically we call them methods. They are basically still functions.

methods vs. functions


#5

That cannot be argued; they are synonymous. The semantics help us distinguish functions that belong to an object or class from free standing functions that can be invoked without calling them on a class or creating an instance. We could technically think of all functions as methods of the window object.

Some silliness, for levity…

 > function foo(){
       this.bar = function() {
           return "bar";
       }
       return "foo";
   }
<- undefined
 > a = new foo()
<- foo {bar: ƒ}
 > a.bar()
<- "bar"
 > window.foo()
<- "foo"
 >