Created
February 24, 2021 03:17
-
-
Save PabloMur/12e436f0386336e7ff5f8afa7bbe589f to your computer and use it in GitHub Desktop.
ejercicio cap 18 - mod 1
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
const textos = [ | |
{ | |
titulo: " un titulo ", | |
texto: "uno dos tres cuatro cinco", | |
}, | |
{ | |
titulo: "1984 ", | |
texto: "es una novela política de ficción distópica", | |
}, | |
{ | |
titulo: " fahrenheit 451", | |
texto: "La novela presenta una sociedad estadounidense del futuro", | |
}, | |
{ | |
titulo: "¿Sueñan los androides con ovejas eléctricas?", | |
texto: | |
" es una novela corta de ciencia ficción del subgénero ciberpunk del autor Philip K. Dick (1928-1982) publicada inicialmente en 1968. Fue adaptada libremente por Ridley Scott en la película Blade Runner de 1982.", | |
}, | |
{ | |
titulo: " yo, robot ", | |
texto: "relatos basados en las tres leyes de la robótica", | |
}, | |
]; | |
function textosCortos(collection) { | |
let arrayTextosCortos = []; | |
for (let i = 0; i < collection.length; i++) { | |
let item = collection[i]; | |
let tituloDelItem = item.titulo; | |
let tituloSinEspacios = tituloDelItem.trim(); | |
let primeraLetra = tituloSinEspacios.charAt(0); | |
let mayuscula = primeraLetra.toUpperCase(); | |
let tituloFinal = tituloSinEspacios.replace(primeraLetra, mayuscula); | |
let textoDelItem = item.texto; | |
let largoDelTexto = textoDelItem.split(" "); | |
if (largoDelTexto.length <= 10) { | |
let objeto = { | |
titulo: tituloFinal, | |
texto: textoDelItem, | |
}; | |
arrayTextosCortos.push(objeto); | |
} | |
} | |
return arrayTextosCortos; | |
} | |
// test: no modificar | |
function testTextosCortos() { | |
const textosDePrueba = [ | |
{ | |
titulo: " un titulo ", | |
texto: "uno dos tres cuatro cinco", | |
}, | |
{ | |
titulo: "what ever", | |
texto: "uno dos tres cuatro cinco seis siete ocho nueve diez once", | |
}, | |
]; | |
const respuesta = textosCortos(textosDePrueba); | |
if (respuesta.length == 1 && respuesta[0].titulo == "Un titulo") { | |
console.log("testTextosCortos passed"); | |
} else { | |
throw "testTextosCortos failed"; | |
} | |
} | |
function main() { | |
testTextosCortos(); | |
console.table(textosCortos(textos)); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jaja me gustó lo del replace! Super bien!