Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
Understand the following exercise of js loops and strings in array

I'm trying to make sense of the following javascript exercise but can't seem to make sense of it, all I know for now is that I can access each string with "acrostic[i]" where the i would be the number in the array but don't know where to go from there.

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);
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

While the other answers are correct, I think they're not beginner-friendly.

If all you need to do for the exercise is to replace the part commented in the for loop, then it's simply:

let georgianaAugustaKeats = "";

for (let i = 0; i < acrostic.length; i += 1) {
   georgianaAugustaKeats += acrostic[i][0];
}
console.log(georgianaAugustaKeats);

NOTE: The third string ends with an empty ,,, I'm not sure that's intentional. That will cause this code to generate an error (because the string is empty, there's no first element). You can easily account for that, but I think it's another question.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use map() with some destructuring to generate an array containing the first letter of every line, and then join() that array into a string:

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);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Array.prototype.reduce() combined with Destructuring assignment

Code:

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)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda