Functional Programming - Implement map on a Prototype

Tell us what’s happening:
Describe your issue in detail here.
I can pass the test with a For Loop, but I can’t pass the test using the forEach method…
… the test is updated but the newest tutorial I can find is from 2020. It’s now almost 2024… I’m going to use the for loop to pass the test; as it is day 4 for me on this test. Hoping a new video or update to the Code Camp Challenge. here is the code that won’t pass the test.

Array.prototype.myMap = function(callback) {
  const newArray = [];
  // Only change code below this line
this.forEach(individualElement => {
  newArray.push(callback(individualElement));
});
  // Only change code above this line
  return newArray;
};

Your code so far

Array.prototype.myMap = function(callback) {
  const newArray = [];
  // Only change code below this line
forEach(individualElement =>{
  newArray.push(callback(individualElement));
})
  
  // Only change code above this line
  return newArray;
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: Functional Programming - Implement map on a Prototype

Link to the challenge:

This isn’t how you use forEach(). forEach() is an array method and needs to be called on an array: Array.prototype.forEach() - JavaScript | MDN


Note that map() callbacks take up to 3 arguments. You are only using 1.

I do understand that; but the test no longer provides an opening Array… the older version of the test does. Not sure why I could pass with the For Loop but not the forEach seeing how both require an array to iterate over.

What do you mean by “an opening Array”? You should not be referencing any global variables in this challenge.

Any video you find that uses a global variable is very, very wrong.

here is the link to the video that Free Code Camp uses a Global Variable for…

and THANK YOU for helping me. It’s been 4 days so far lol

seems as though I can’t post the link here… Useful Programmer link

Don’t worry about copying somebody else’s code. Reading somebody else’s code is a different skill than writing your own code. Reading code is important but it doesn’t teach you how to write code.


This here is your big problem.

the global variable is from Free Code Camps old test… the updated lesson has no global variable.

Right, the global variable was removed because you don’t need it. Don’t use global variables inside of functions 99% of the time.

my for loop used (this[i], i, this) an I passed it…

the forEach I only used (callback(indiviualElement)) , …

what would the correct forEach look like?

You should invoke the callback the exact same way in both cases. So how do you get i and this in a forEach loop?

I’m lost. Sorry to waste your time.
Have a great day!

You aren’t wasting my time.

Did you read the link I posted?

You said that you

You need to use i and this when you are using forEach)( as well because map() and myMap() both require up to 3 arguments. You are only using 1 here

So you need to pass i and this while using the forEach() loop.

The following parameters are available to the forEach() loop callback function:

callbackFn
A function to execute for each element in the array. Its return value is discarded. The function is called with the following arguments:

element
The current element being processed in the array.

index
The index of the current element being processed in the array.

array
The array forEach() was called upon.

Do any of these look like they might do what i and this are doing in your for loop solution?

2 Likes

been studying since 6a.m.
think my brain is jelly;
lunch break

it’s the syntax of the 3…

for(let i = 0; i < this.length; i ++){
this.newArray.push(callback(this[i], i , this));
}
return newArray;

I got it… the forEach() syntax I don’t.

You should take a break.

Then come back and read carefully.

Here, you are passing callback(this[i], i, this)

Here you are only passing individualElement.

So… you need to add i and this. Can you say what i and this means in your for loop solution?

If you read this article I keep linking for you to read:

The callback function of forEach() has three possible values you can use

I have been reading the same reference article you posted. Thank You Jeremy! I can’t tell you how grateful I am for your help.
I am taking a lunch break.
I do understand that we are talking about (this element) and (this index) and (this Obects instance) …

You are pretty close. You have all the pieces there

this is the syntax that’s still a bit confusing…

this.forEach((element, index, originalArr) =>
newArray.push(callback(element, index, originalArr))

… in a for loop I just type this[i], i, this in the loop
but in a forEach( ) I’m seeing words like, element, index and the most confusing , originalArr … I know javascript recognizes this and [i] an i , but does this mean that Element Index and originalArr are going to be the standard for a forEach?

need to see it applied in a practical usage … totally kicking my butt

You could use similar words in both loops. Here is your for-loop code written with different words:

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

There is no special meaning to the exact words (except for this). You should pick words for the variable names that explain what they are, but the words you pick don’t make the code behave differently.

Ok, I totally follow what you are saying here…
love me some for loop …

could you show your example using the forEach method?

You should try writing it yourself