Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save afaqahmedkhan/1d66a063725506b59419906acd8f7654 to your computer and use it in GitHub Desktop.
Save afaqahmedkhan/1d66a063725506b59419906acd8f7654 to your computer and use it in GitHub Desktop.
Functional Programming: Implement map on a Prototype
Functional Programming: Implement map on a Prototype
As you have seen from applying Array.prototype.map(), or simply map() earlier, the map method returns an array of the same length as the one it was called on. It also doesn't alter the original array, as long as its callback function doesn't.
In other words, map is a pure function, and its output depends solely on its inputs. Plus, it takes another function as its argument.
It would teach us a lot about map to try to implement a version of it that behaves exactly like the Array.prototype.map() with a for loop or Array.prototype.forEach().
Note: A pure function is allowed to alter local variables defined within its scope, although, it's preferable to avoid that as well.
Write your own Array.prototype.myMap(), which should behave exactly like Array.prototype.map(). You may use a for loop or the forEach method.
// the global Array
var s = [23, 65, 98, 5];
Array.prototype.myMap = function(callback){
var newArray = [];
// Add your code below this line
for(let i = 0; i < this.length ; i++ ){
newArray.push(callback(this[i]));}
// Add your code above this line
return newArray;
};
var new_s = s.myMap(function(item){
return item * 2;
});
@xuantruong091196
Copy link

If arr have more object, How will you handle it???

@Rupesh-Darimisetti
Copy link

Much better answer than above solution to the problem - [ Functional Programming: Implement map on a Prototype ]

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

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

const new_s = s.myMap(function(item) {
  return item * 2;
});
console.log(new_s);

@bridude
Copy link

bridude commented Sep 4, 2022

@Rupesh-Darimisetti yours is incorrect. you're doing the function in the map method itself. the value of new_s should affect the callback, not the other way round. thus, the solution above yours is still the answer.

@gabrielmanoel-hub
Copy link

var array1 = [23, 65, 98, 5, 13]
var array2 = ["naomi", "quincy", "camperbot"]
var array3 = [1, 1, 2, 5, 2]

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

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

var new_s2 = array2.myMap(function(item){
return item.toUpperCase();
});
var new_s3 = array3.myMap(function(itens, index, array){
return array[index + 1] || array[0];
});

@gabrielmanoel-hub
Copy link

I would like to know if this line of reasoning would be correct for solving the exercise

@Priyas-20
Copy link

Yeah @gabrielmanoel-hub , among all the provided solution, your solution would definetely work for all the test cases.

@HusnainButt12
Copy link

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

// Only change code above this line
return newArray;

};

@PDGONZALE1
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment