Last active
April 29, 2020 04:36
-
-
Save AlvisonHunterArnuero/28025ef8783774e4bd4a52d868d8d1be to your computer and use it in GitHub Desktop.
Unos ejercicios basados en el uso de los helpers que me gustaria subir a gist para tenerlos ahi como referencia para mi y para otros
This file contains 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
// Using Array Helpers Excercise - This uses Filter() | |
const numbers = [10, 20, 30]; | |
function reject(){ | |
let lessThanFifteen = numbers.filter(function(number){ | |
return number < 15; | |
}); | |
return ` Number that is less than 15 is: ${lessThanFifteen}`; | |
} | |
reject(); | |
// Another Helper Excercise - This uses Find() | |
const ladders = [{ id: 1,height: 20 },{ id: 2, height: 30 },{ id: 3, height: 25 }]; | |
function findWhere(array, criteria) { | |
let property = Object.keys(criteria)[0] ; | |
var results = array.find(function(result){ | |
return result[property] === criteria[property] ; | |
}); | |
return `Item # ${results.id} is a ladder of ${results.height} feet of height.`; | |
} | |
findWhere(ladders, { height: 25 }); | |
// Another one for helpers this one is with reducer() | |
const desks = [ { type: 'sitting' },{ type: 'standing' },{ type: 'sitting' },{ type: 'sitting' }, { type: 'standing' }]; | |
let deskTypes = desks.reduce(function(total,desk) { | |
desk.type === 'sitting' ? ++total.sitting : ++total.standing; | |
return total | |
},{ sitting: 0, standing: 0 }); | |
deskTypes; | |
// Another one for helpers this one is with reducer() | |
const numeros = [1, 1, 2, 3, 4, 4] | |
function ElementoUnico(array) { | |
return array.reduce((arr, num) => { | |
if(arr.indexOf(num) === -1) { | |
arr.push(array.find((n) => n === num)) | |
} | |
return arr; | |
}, []) | |
} | |
ElementoUnico(numeros);; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment