Estoy tratando de dar sentido al siguiente ejercicio de javascript, pero parece que no puedo entenderlo, todo lo que sé por ahora es que puedo acceder a cada cadena con "acróstico [i]" donde i sería el número en la matriz, pero no sé a dónde ir desde allí.
const acrostic = [ "Give me your patience, sister, while I frame", "Exact in capitals your golden name;",, "Or sue the fair Apollo and he will", "Rouse from his heavy slumber and instill", "Great love in me for thee and Poesy.", "Imagine not that greatest mastery", "And kingdom over all the Realms of verse,", "Nears more to heaven in aught, than when we nurse", "And surety give to love and Brotherhood.", " ", "Anthropophagi in Othello's mood;", "Ulysses storm'd and his enchanted belt", "Glow with the Muse, but they are never felt", "Unbosom'd so and so eternal made,", "Such tender incense in their laurel shade", "To all the regent sisters of the Nine", "As this poor offering to you, sister mine.", " ", "Kind sister! aye, this third name says you are;", "Enchanted has it been the Lord knows where;", "And may it taste to you like good old wine,", "Take you to real happiness and give", "Sons, daughters and a home like honied hive." ]; /* Declare a variable that will return the final string */ let georgianaAugustaKeats = "acrostic[i][0]"; for (let i = 0; i < acrostic.length; i += 1) { /* add each first character of each string to the array to the georgianaAugustaKeats variable*/ } console.log(georgianaAugustaKeats);Si bien las otras respuestas son correctas, creo que no son aptas para principiantes.
Si todo lo que necesita hacer para el ejercicio es reemplazar la parte comentada en el ciclo for , entonces es simplemente:
let georgianaAugustaKeats = ""; for (let i = 0; i < acrostic.length; i += 1) { georgianaAugustaKeats += acrostic[i][0]; } console.log(georgianaAugustaKeats); NOTA: La tercera cadena termina con un ,, , no estoy seguro de que sea intencional. Eso hará que este código genere un error (porque la cadena está vacía, no hay un primer elemento). Puede explicarlo fácilmente, pero creo que es otra pregunta.
Puede usar map() con algo de desestructuración para generar una matriz que contenga la primera letra de cada línea y luego join() esa matriz en una cadena:
const acrostic = [ "Give me your patience, sister, while I frame", "Exact in capitals your golden name;", "Or sue the fair Apollo and he will", "Rouse from his heavy slumber and instill", "Great love in me for thee and Poesy.", "Imagine not that greatest mastery", "And kingdom over all the Realms of verse,", "Nears more to heaven in aught, than when we nurse", "And surety give to love and Brotherhood.", " ", "Anthropophagi in Othello's mood;", "Ulysses storm'd and his enchanted belt", "Glow with the Muse, but they are never felt", "Unbosom'd so and so eternal made,", "Such tender incense in their laurel shade", "To all the regent sisters of the Nine", "As this poor offering to you, sister mine.", " ", "Kind sister! aye, this third name says you are;", "Enchanted has it been the Lord knows where;", "And may it taste to you like good old wine,", "Take you to real happiness and give", "Sons, daughters and a home like honied hive." ]; const result = acrostic.map(([first]) => first).join(''); console.log(result);Puede usar Array.prototype.reduce() combinado con la asignación de desestructuración
Código:
const acrostic = [ 'Give me your patience, sister, while I frame', 'Exact in capitals your golden name;', , 'Or sue the fair Apollo and he will', 'Rouse from his heavy slumber and instill', 'Great love in me for thee and Poesy.', 'Imagine not that greatest mastery', 'And kingdom over all the Realms of verse,', 'Nears more to heaven in aught, than when we nurse', 'And surety give to love and Brotherhood.', ' ', "Anthropophagi in Othello's mood;", "Ulysses storm'd and his enchanted belt", 'Glow with the Muse, but they are never felt', "Unbosom'd so and so eternal made,", 'Such tender incense in their laurel shade', 'To all the regent sisters of the Nine', 'As this poor offering to you, sister mine.', ' ', 'Kind sister! aye, this third name says you are;', 'Enchanted has it been the Lord knows where;', 'And may it taste to you like good old wine,', 'Take you to real happiness and give', 'Sons, daughters and a home like honied hive.', ] /* Declare a variable that will return the final string */ const result = acrostic.reduce((a, [f]) => a + f, '') console.log(result)