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
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.
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.
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) …
… 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.