Last active
May 1, 2020 02:02
-
-
Save robertoandres24/96bf6cdfa320f3b880decd9cc3aa6bf5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let animals = [ | |
{ name: 'alf', type: 'dog' }, | |
{ name: 'beto', type: 'cat' }, | |
{ name: 'carl', type: 'dog' }, | |
{ name: 'dan', type: 'fish' } | |
] | |
let isDog = a => a.type === 'dog' | |
const myFilter = function (cb) { | |
const newArr = [] | |
for (let i = 0; i < this.length; i++) { | |
let truthy = cb(this[i]) | |
truthy && newArr.push(this[i]) | |
} | |
return newArr | |
} | |
Array.prototype.myFilter = myFilter | |
console.log(animals.myFilter(isDog)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const strArray = ['JavaScript', 'Python', 'PHP', 'Java', 'C']; | |
function mapForEach(arr, fn) { | |
const newArray = []; | |
for(let i = 0; i < arr.length; i++) { | |
newArray.push( | |
fn(arr[i]) | |
); | |
} | |
return newArray; | |
} | |
const lenArray = mapForEach(strArray, function(item) { | |
return item.length; | |
}); | |
// prints [ 10, 6, 3, 4, 1 ] | |
console.log(lenArray); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment