JS Functional Programming

What is going on here, everything has been chill and a little bit complicated at times. But, suddenly this module just seems to be off on a different planet and assuming lots of prior knowledge which wasnt covered.

Is there a separate guide/resource I should be using to explain these concepts at this point?

Do you have an example of something that seems to have lots of prior knowledge that was not included in the curriculum?

The one I am specifically talking about is on number 8 - using array.prototypes, and one of the prior ones was about prototypes too, but they seem to be in a completely different context to the object prototypes used in the previous module.

There isn’t one called ‘number 8’… Do you mean this one?

I don’t exactly love this challenge, but an array is an object, so it has a prototype like any other object.

Yeah this is the one, apologies for not using the lesson title.

It just seems to jump from the previous map one and not link the two, does it want me to create my own map function without using the ‘map’ method?

Yea, that’s the idea. You need to add a new method to the prototype for all arrays called myMap(), and this function should return a new array that has contents given by applying the callback() function to the contents of this instance of an array.

Just for clarification, is the callback(), the function which is written as part of myMap(), or is the callback the function which is declared as part of ‘const new_s’ (the *2 function.?

The callback is completely separate from the definition of myMap(), that’s why .map() is so useful.

In this case, we have

s.myMap(function(item) {
  return item * 2;
});

so

const callback = function(item) {return item * 2;};

or

const callback = (item) => item * 2;

So, both myMap() and .map() should produce new_s == [46, 130, 196, 10].

But, the callback() passed in to .map() or .myMap() can be any function that takes an array item and returns a new value to place in the new array. If we use this other callback function

s.myMap(function(item) {
  return item * 3;
});

then new_s == [69, 195, 294, 15].

1 Like

Thank you, I have been banging my head against a wall and started the module again reading a lot of details about each one with no pressure… it clicked! Thanks for the help on this one. It is a little bit mind-bending at first but it’s great when learning stuff and something finally clicks! Let’s see what I end up banging my head against next!

1 Like

Congrats! It’s mind bending, but super powerful once you understand how to pass functions around like other variables.