Array.prototype.myFilter = function(callback) { const filterArr = []; for(let index = 0; index<this.length; index++) { if(!!callback(this[index], index, this)) { filterArr.push(this[index]); } } return filterArr; } Array.prototype.myReduce = function(callback, accumulator) { if(this.length < 1) { throw new Error("Array is Empty") } if(!accumulator) { if(typeof this[0] === "string") { accumulator = ''; } else if(typeof this[0] === "number") { accumulator = 0; } } for(let index=0; index < this.length; index++) { accumulator = callback(accumulator, this[index]); } return accumulator; } const names = ['Zakir', 'Rashid', 'Harish']; const filterNames = names.myFilter(name => name !== 'Zakir'); const statment = names.myReduce((acc, ele) => acc + ele); console.log(filterNames) // [ 'Rashid', 'Harish' ] console.log(statment); // Zakir Rashid Harish
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Discussion on: Implementing our own Array.map() method in javascript
For further actions, you may consider blocking this person and/or reporting abuse