Functional Prototypes - help needed

Hi,

I could use some help. I’ve been through the prototype lessons on FCC many, many times, but I cannot make heads or tails of this kind of problem. (Not looking for the answer here). Anyone aware of a YouTube vid or website that explains this specific kind of problem in detail?

Thanks.

// the global Array
var s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback){
  var newArray = [];
  // Add your code below this line
    this.newArray()
  // Add your code above this line
  return newArray;

};

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

Do you have issues with understanding what the challenge asks from you, or understanding the implementation?

And, what have you tried so far?

Perhaps both. When I’ve gone through the FCC prototype lessons, I get it. But they’re soooo basic.

I don’t get the connection b/t the var new_s and how I’m supposed to use it with Array.prototype.myMap.

Even after looking at the answer, I don’t get it. I just need a very granular explanation.

I have scoured YouTube for a video that covers something like this, but no luck.

You are creating a method myMap() which does have to have the same effect as the native map() method

Inside the method you are creating (the prototype) you can reference the array on which the method is applied with this, in this case the array is s (and new_s will be the one obtained from the method - but don’t reference nor s nor new_s isndie the prototype!)
So, you need to have a method that return an array in which each element is obtained from the original array changed through the callback.

What are you trying to do with this.newArray()? newArray is not a function!

Thanks for the link and the explanation. I know I’m irritated b/c this has really hung me up. Blazed through the ReactJs and other modules, but for the life of me cannot make any sense of this. Watched a lot of YouTube vids on prototypes and have yet to run into a similar kind of problem. I’ll just spend another week/month or however long it takes until I get it.

Prototypes are not that easy to understand conceptually, true, but you don’t need the deep theoretical understanding to complete this.
what exactly is your issue here?

If the instruction was to create a function that takes an array and a callback, and that it returns an array where each element was changed using the callback, would you be able to?

var s = [23, 65, 98, 5];

const mapping = function (array, callback) {
let newArray = [];
// change code below this line

// each element of newArray is an element of array changed through the callback

// change code above this line
return newArray; }

let new_s = mapping(s, function(item){
  return item * 2;
});

Anyway, I can suggest the Khan Academy - computer programming lessons, there the part on functional prototype was explained in a simpler way, it may help you

This is not correct if you went through the the React section: if that is the case you have done this exact same thing multiple times. If I rephrase this:

Can you write your own implementation of this function?

Component.prototype.render = function () {
}

Or, same thing:

The render method is currently defined something like this:

    Component.prototype.render = () => throw new Error('A component must have a `render` method defined');

Define your own render method on the prototype of your component so that it doesn’t throw an error.

    class MyComponent extends React.Component {
      render() {
        // Your code here
      }
    }

With react, any function you define or redefine on your component is put on the prototype of that component. Within the code that defines that component, this should refer to that component. Similarly, within the code you are putting on the prototype of the array, this refers to the array.