Skip to content

Instantly share code, notes, and snippets.

@seba-campo
Created January 17, 2022 14:03
Show Gist options
  • Save seba-campo/d4ee90cf9bb9a0e4bf98726884c1ec53 to your computer and use it in GitHub Desktop.
Save seba-campo/d4ee90cf9bb9a0e4bf98726884c1ec53 to your computer and use it in GitHub Desktop.
Ejercicio de métodos de array del capitulo 21 modulo 1
// todas las funciones se deben resolver en una linea de código (máximo 2)
function crearArrayDeObjetos(arrayDeNumeros, nombreDePropiedad) {
// si recibe [1,2,3,4] y "a"
// debe devolver
// [{ a:1 }, { a:2 }, { a:3 }, { a:4 }]
// usar método map
const arrayNuevo = arrayDeNumeros.map(function(item){
return {[nombreDePropiedad]: item}
})
return arrayNuevo
}
function filtrarPor(arrayDeObjetos, nombreDePropiedad, primeraLetra) {
// si recibe [{ a: 100, unTexto: "hola" }, { a: 34, unTexto: "chau" }]
// y "unTexto" como nombreDePropiedad
// y "c" como primeraLetra
// debe devolver
// [{ a: 34, unTexto: "chau" }]
// usar método filter
const filtrado = arrayDeObjetos.filter((el) => {
return el[nombreDePropiedad].startsWith(primeraLetra)
});
return filtrado
}
function buscarPorProp(arrayDeObjetos, nombreDePropiedad, valorABuscar) {
// si recibe [{ a: 100, unTexto: "hola" }, { a: 34, unTexto: "chau" }]
// y "unTexto" como nombreDePropiedad
// y "chau" como valorABuscar
// debe devolver
// { a: 34, unTexto: "chau" }
// usar método find
const encontrado = arrayDeObjetos.find((el) => {
return el[nombreDePropiedad] && el[nombreDePropiedad] == valorABuscar;
});
return encontrado
}
function ordenarPor(arrayDeObjetos, nombreDePropiedad) {
// si recibe [{ a: 100, unTexto: "hola" }, { a: 34, unTexto: "chau" }]
// y "a" como nombreDePropiedad
// debe devolver
// [{ a: 34, unTexto: "chau" }, { a: 100, unTexto: "hola" }]
// usar un el método sort
const ordenado = arrayDeObjetos.sort(function(a,b){
if(a[nombreDePropiedad] < b[nombreDePropiedad]){
return -1
}
if(a[nombreDePropiedad] > b[nombreDePropiedad]){
return 1
}
if(a[nombreDePropiedad] == b[nombreDePropiedad]){
return 0
}
})
return ordenado
}
// no modificar el test
function testMetodos() {
const arrayDeNumeros = [97, 78, 23, 12, 66];
const arrayDeObjetos = crearArrayDeObjetos(arrayDeNumeros, "a");
if (
arrayDeObjetos[0].a == arrayDeNumeros[0] &&
arrayDeObjetos[1].a == arrayDeNumeros[1] &&
arrayDeObjetos[2].a == arrayDeNumeros[2] &&
arrayDeObjetos[3].a == arrayDeNumeros[3] &&
arrayDeObjetos[4].a == arrayDeNumeros[4]
) {
console.log("crearArrayDeObjetos passed");
} else {
throw "crearArrayDeObjetos not passed";
}
// ######################################################
arrayDeObjetos[0].texto = "ah re";
arrayDeObjetos[1].texto = "ah re";
arrayDeObjetos[2].texto = "ah re";
arrayDeObjetos[3].texto = "vamo a codear";
arrayDeObjetos[4].texto = "vamo a codear";
const filtrado = filtrarPor(arrayDeObjetos, "texto", "a");
if (
filtrado[0].texto == "ah re" &&
filtrado[1].texto == "ah re" &&
filtrado[2].texto == "ah re"
) {
console.log("filtrarPor passed");
} else {
throw "filtrarPor not passed";
}
// ######################################################
const encontrado = buscarPorProp(arrayDeObjetos, "a", 97);
if (typeof encontrado.texto == "string" && encontrado.a == 97) {
console.log("buscarPorProp passed");
} else {
throw "buscarPorProp not passed";
}
// ######################################################
const sortedByA = ordenarPor(arrayDeObjetos, "a");
if (
sortedByA[0].a == 12 &&
sortedByA[1].a == 23 &&
sortedByA[2].a == 66 &&
sortedByA[3].a == 78 &&
sortedByA[4].a == 97
) {
console.log("ordenarPor passed");
} else {
throw "ordenarPor not passed";
}
}
function main() {
testMetodos();
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment