我正在嘗試理解以下 javascript 練習,但似乎無法理解,我現在所知道的是我可以使用 "acrostic[i]" 訪問每個字串,其中 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);
uj5u.com熱心網友回復:
您可以使用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);
uj5u.com熱心網友回復:
您可以將Array.prototype.reduce()與Destructuring assignment結合使用
代碼:
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)
uj5u.com熱心網友回復:
雖然其他答案是正確的,但我認為它們對初學者不友好。
如果您需要為練習做的只是替換for回圈中注釋的部分,那么它很簡單:
let georgianaAugustaKeats = "";
for (let i = 0; i < acrostic.length; i = 1) {
georgianaAugustaKeats = acrostic[i][0];
}
console.log(georgianaAugustaKeats);
注意:第三個字串以空結尾,,,我不確定這是故意的。這將導致此代碼生成錯誤(因為字串為空,因此沒有第一個元素)。你可以很容易地解釋這一點,但我認為這是另一個問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/481214.html
標籤:javascript 数组 for循环
