Home > Software design >  Understand the following exercise of js loops and strings in array
Understand the following exercise of js loops and strings in array

Time:05-27

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

CodePudding user response:

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

CodePudding user response:

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)

CodePudding user response:

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.

  • Related