Functions question

Hi - a few questions about functions.

  1. why is this problem using myMap not map? I don’t understand it’s significance.

  2. which solution type should I be focused on? Initially when I was looking at arrows and some other es6 topics, I was trying to learn algorithms and so I didn’t really focus a lot on each concept (also there wasn’t enough practice with them). Now I’m seeing that varies problems focus on es6. For example one solution to this is this.forEach(a => newArray.push(callback(a)));. I thought to myself ideally I’d learn all, but of course that’s not easy when I’m starting out so I thought it’d be worth asking.

  3. I may have asked this before but is there a better alternative than mozilla docs? As a beginner I feel like I’m reading a textbook as there are so many ways each thing could be used. I just want to learn the part about my specific issue so I can learn based on this course. For example, a place with a topic navigation would be nice vs reading the entire page and trying to determine which definition is appropriate.

    Your code so far


// The global variable
var s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback) {
var newArray = [];
// Only change code below this line
for (let i=0; i < this.length; i++) {
  newArray.push(callback(this[i]))
}
// Only change code above this line
return newArray;
};

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

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36.

Challenge: Implement map on a Prototype

Link to the challenge:

This question is asking you to implement map. But map is already defined, so they are asking you to write myMap which will do the same thing as map.

I’m not sure what you mean. I would get something simple that works and then focus on fancy syntax. The challenge text suggests using .forEach. Do you know how a .forEach works?

Not really. You can find tutorials that may happen to answer your questions, but MDN is the single best comprehensive source of how any piece of JavaScript works. Basically, its the single place that has the examples you need. Without it you’d probably just Google around to find similar examples spread across a bunch of websites.

1 Like

You mean already defined in the question, but not in the code, correct?

Well it says for loop or .forEach but yeah.

Yeah I mean I like that the information is reliable, but it’s tough because there’s so many cases with definitions distinguishing each (ie derived classes - no idea what this is). What I mean by that is that when I want to check the this page for example, then I’m shown a long page with varies contexts, examples, and methods. It’s hard to know where to go to find the simple part I’m learning or need help on.

The MDN docs are the gold standard of documentation, but sometimes you may need a resource that focuses on why you would use something or how you might see it in real life. There isn’t any single good source for that. You just have to comb through blog articles and StackOverflow posts (and the freeCodeCamp forum, of course) about your specific question.

3 Likes

What Jeremy means is that Array.map() is a builtin array method in JavaScript.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

No. I mean in the code. Every single array has the map method defined automatically.

So I’d start there. Pick one. Now what? What do you want the code to do? What is the input? What is the output?

I can definetly relate to this as a beginner. There were plenty of times where I would look up documentation and be confused.

But that is because we are still beginners and it is all new to us.

Good news, there are tons of articles and videos catered to beginners that break this stuff down.

For these concepts in particular I would suggest Programming with Mosh’s youtube channel or Brad Traversey’s channel. They do a good job of explaining things for beginners.

But I wouldn’t discount MDN docs cause it really is a good source.

1 Like

@JeremyLT @ArielLeslie ah got it, so it’s built in without the need to define it.

@jwilkins.oboe funny thing is I just watched Mosh’s functions video and it helped out a lot. If I’m just looking at fcc unfortunately there are times the concepts don’t stick to my memory. The algorithm section was actually good because it required deeper solutions and resulted in better memorization for me. The exercises that just explain a concept with a easy plug in solution are easily forgettable

1 Like

Well you are just going to love the next algorithm section :grinning:
But when you get though it, you will feel like you climbed a mountain

1 Like

Can you actually explain this problem to me? I got a better sense of functions and callbacks (I think) and I even redid this functional programming section up to this point, but I don’t feel comfortable with using them.

Is the following function

Array.prototype.myMap = function(callback) {
  var newArray = [];
  for (let i=0; i < this.length; i++) {
    newArray.push(callback(this[i]))
  }
  return newArray;
};

used inside of this one

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

because of the insertion of .myMap = function (callback) alone?

Creating functions and using them elsewhere wasn’t really covered well imo

Right here, you are defining the function myMap on the Array prototype, making this a .whateverThingymabob method on all arrays.

This says that the myMap method accepts one argument, the callback function.

Here you are calling myMap on your array s.

This is the callback function you are using with your call to myMap.


Whenever you hear the term ‘high order methods’ or ‘high order functions’ it just means “a function that takes a function as an argument”. In this case, your myMap method take a callback function that describes how to create the a new array from the old array’s entries (or how to ‘map’ the old entries into new entries).

3 Likes

Got it thank you so much!

1 Like