Divisible by 2?

There is only one function definition.

s.myFilter(...) is a function call, not a function definition.

The criteria comes from the function you pass in as the argument to myFilter

In this case, this function returns true when the item is an odd integer.

I understand it’s true wen it’s odd, but does the entire thing execute together at the same exact time?

I don’t understand this

You’re saying two separate arrays but both need each other and you’re saying one run before the other. Is there a breakdown of how this entire problem works?

myFilter is a function you have defined on the prototype for all Arrays, which means it is called with dot notation.

You called s.myFilter(...). Right there you call the function myFilter on the array s with the function that only return true for odd items.

This returns the array with only odd items that you save to new_s.

Ok yeah soo myFilter runs through array s, but how is it connected to s.myFilter and does is work simultaneously?

If it’s connected because s.myFilter then which part is using

(function(item){
  return item % 2 === 1;

I just don’t understand how both are plugged in to each other. I redid this entire section and I don’t understand how we went from simple functions like

function greet(name) {
console.log('Hello ' + name));
}
greet('John');

to this

I don’t know what you mean by ‘work simultaniously’.

It can be helpful to think of methods like

someArray.myFilter(callbackFunction)

as working like

myFilter(someArray, callbackFunction)

That is to say, the method that is associated with an array or object implicitly takes the array or object as an argument, via this in the method (or function, if you prefer) definition.

I don’t understand. Is it normal for functions to feel 5x harder than algorithms?

What part don’t you understand? I’m not sure which part to clarify.

High order functions are tricky. It can be difficult to wrap your head around functions that take other functions as arguments

Probably, I should ask how well you feel like you understand what .filter() does. This challenge is asking you to replicate the .filter() method.

I just don’t understand how Array.prototype.myFilter and var new_s are working together.

I understand new_s is taking Array.prototype.myFilter using .myFilter, but not how it’s basically combining to use the remainder result at the point of if(callback(this[i])) to determine when to push this[i]

new_s is the result after using .myFilter() on the array s. It is only a variable holding the result. They are not working together in any way.

The filter (or myFilter) is a method defined on an array. In a filter method, you accept as an argument a function that returns true when passed an item that you want to retain. This method returns an array that contains only the elements that return true with this function. You can change this function each time you call the method. In this example, we use a function that returns true on odd numbers.


Internally, both filter and myFilter make a new array, push onto that array only elements that return true, and return that new array from the method.

Ok if they’re separate Array.prototype.myFilter doesn’t have a argument to determine what is true, does it?

I’ve now stared at this for an hour. Is there anywhere with a full breakdown without paragraphs? I don’t think I’ll understand this without one.

Right here is the argument list for Array.prototype.myFilter (same argument an .filter). This callback function determines what is true.


const someArray = [1, 2, 3, 5, 6];
const oddArray = someArray.filter(function callbackFunction(item) {
    return item % 2 === 1
  });

The argument callbackFunction is passed to the method (function) filter.

Internally, the method filter makes a new array that contains only items that return true when the item is odd.

This returned array is saved to the oddArray variable.

I’m gonna try again to understand this tomorrow but I am not understanding

This problem with this example is it’s more simple just like how this is

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

So when it says you’ll understand if you make your own filter, it’s like no I understand this but not the problem you’re giving me. It’s also frustrating because I’m getting arrows, functions, weird function names (item) all mixed in. I’m gonna look at this again tomorrow, really wanted to figure this out before I slept :confused:

const someArray = [1, 2, 3, 5, 6];
const oddArray = someArray.filter(function callbackFunction(item) {
    return item % 2 === 1
  });

is the same as

var s = [1, 2, 3, 5, 6];
var new_s = s.myFilter(function(item) {
return item % 2 === 1;
});

or

const someArray = [1, 2, 3, 5, 6];
const oddArray = someArray.filter(item => item % 2 === 1);

It is the same idea with different syntax.


const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

Same idea here, but now the new array result contains everything that returns true from the callback function, which is items that are longer than 6 characters.


The goal of this challenge was for you to make a method for all arrays that filters, which is to say, it returns a new array than only contains elements of the original array that return true from a callback function passed in as an argument.

There are several ways to define a function that will be passed in as a callback function in JavaScript, so don’t let that distract you.

The key ideas are

  1. you write a method for all arrays by defining a method on Array.prototype
  2. you access the array the method is being called upon with this in the method definition
  3. the method takes a single parameter callback which returns true if the item is to be retained
  4. you need to make a new array that only contains the items from the original array (referenced by this) which return true when passed into the callback function
  5. the filter method needs to return this new array that only contains the elements that return true when passed to the callback function

Ways to write functions:

function myFunciton(args) {
  // body
  return whatever;
}
// Or
const myFunction = function(args) {
  // Body
  return whatever;
}
// Or
const myFunction = (args) => {
  // Body
  return whatever;
}
// Or a few other variations

It seems like there is a missing piece of knowledge somewhere, but I’m not sure where. I’m sure one of these explanations will zero in on it eventually, but in may take some more back and forth.

Thank you for the explanation. The for loop is part of the method for all arrays. The for loop contains a callback (which just means it’s able to use another function instead of callback?), then in this case it’s using the filter function return item % 2 === 1 ? Then it looks like this?

Array.prototype.myFilter = function(callback) {
// Only change code below this line
var newArray = [];
for (let i = 0; i < this.length; i++) {
if(this[i] % 2 === 1 {
newArray.push((this[i]));
}
}

You said these don’t use each other also so I’m confused. I also can’t console.log newArray so I don’t know what that does on it’s own without a argument.

I’ve watched several videos on functions to make sure I understand, but it’s not helping figure this out.

The callback is the ‘another function’ it uses. We pass in the callback function as an argument, and internally to the filter or myFilter method we use the name ‘callback’ for that function.

You should be able to add a console log before the myFilter method returns. I’m not sure what ‘does on its own without an argument’ means. Functions do nothing on their own unless they are called with arguments.

Ok the callback(this[i]) is being passed into new_s in place of item because of .myFilter correct?

If I’m correct, can you share another example with me? Unfortunately, I still don’t feel strongly about functions taking other functions and maybe the Pseudocode used in functions

Also why can’t we just use this

var s = [23, 65, 98, 5];
var new_s = s.filter(number => number % 2 === 1);

I just want to clarify - new_s just happens to be what we are calling the output. The variable is irrelevant to the function; it’s just were we store the output.

We are ‘just’ doing that, but with slightly different syntax. The point of the exercise is to make a new function which replicates the behavior of filter. So you can use this new function the exact same way you can use filter.

Ok I’m gonna move on at this point but can you help me find where functions taking other functions was covered? Or will it be covered more? I don’t truly understand this so I need to learn this some other way.