理解下面的js循环和数组中的字符串的练习



我试图让下面的javascript练习有意义,但似乎不能让它有意义,我现在所知道的是,我可以访问每个字符串与"这里的I应该是数组中的数字,但不知道从哪里开始。

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

虽然其他答案是正确的,但我认为它们对初学者不太友好。

如果练习中需要做的只是替换for循环中注释的部分,那么只需:

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

注意:第三个字符串以空,,结束,我不确定这是故意的。这将导致该代码生成一个错误(因为字符串为空,没有第一个元素)。你可以很容易地解释那件事,但我认为那是另一个问题。

您可以使用map()和一些解构来生成包含每行首字母的数组,然后join()将该数组转换为字符串:

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

可以将Array.prototype.reduce()与Destructuring赋值结合使用

代码:

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)

最新更新