字串 1
在我們的第一個字串任務中,我們從小處著手。您已經有一個名為 quoteStart 的變數,其中包含一句名言的一半,我們需要您將其補全。
完成任務
- 查詢這句名言的另一半,並將其新增到一個名為
quoteEnd的變數中。 - 將這兩個字串連線起來,形成一個包含完整名言的單一字串。將結果儲存在一個名為
finalQuote的變數中。 - 您會發現此時會出現一個錯誤。您能修復
quoteStart的問題,使完整的名言正確顯示嗎?
js
const quoteStart = 'Don't judge each day by the harvest you reap ';
// Add your code here
// Don't edit the code below here!
const section = document.querySelector("section");
const para1 = document.createElement("p");
para1.textContent = finalQuote;
section.appendChild(para1);
點選此處顯示解決方案
你完成的 JavaScript 應該看起來像這樣
js
// You need to escape the quote
const quoteStart = 'Don\'t judge each day by the harvest you reap ';
const quoteEnd = "but by the seeds that you plant.";
const finalQuote = `${quoteStart}${quoteEnd}`;
// Don't edit the code below here!
// ...
字串 2
在此任務中,您將獲得兩個變數,quote 和 substring,它們都包含字串。
完成任務
- 檢索名言的長度,並將其儲存在名為
quoteLength的變數中。 - 找到
substring出現在quote中的索引位置,並將該值儲存在名為index的變數中。 - 使用您擁有的變數以及可用的字串屬性/方法,將原始名言擷取為“I do not like green eggs and ham.”(我不喜歡綠色的雞蛋和火腿。)並將其儲存在名為
revisedQuote的變數中。
js
const quote = "I do not like green eggs and ham. I do not like them, Sam-I-Am.";
const substring = "green eggs and ham";
// Don't edit the code above here!
// Add your code here
// Don't edit the code below here!
const section = document.querySelector("section");
section.innerHTML = " ";
const para1 = document.createElement("p");
para1.textContent = `The quote is ${quoteLength} characters long.`;
const para2 = document.createElement("p");
para2.textContent = revisedQuote;
section.appendChild(para1);
section.appendChild(para2);
點選此處顯示解決方案
你完成的 JavaScript 應該看起來像這樣
js
// ...
// Don't edit the code above here!
const quoteLength = quote.length;
const index = quote.indexOf(substring);
const revisedQuote = quote.slice(0, index + substring.length + 1);
// Don't edit the code below here!
// ...
字串 3
在下一個字串任務中,您將獲得與上一個任務中最終得到的名言相同的內容,但它有些錯誤!我們需要您修復並更新它。
完成任務
- 將大小寫更改為正確的句子大小寫(除首字母大寫外,其餘全部小寫)。將新的名言儲存在名為
fixedQuote的變數中。 - 在
fixedQuote中,將“green eggs and ham”(綠色的雞蛋和火腿)替換為您真正不喜歡的另一種食物。 - 還有一個小改動要做——在名言末尾新增一個句號,並將最終版本儲存在名為
finalQuote的變數中。
js
const quote = "I dO nOT lIke gREen eGgS anD HAM";
// Don't edit the code above here!
// Add your code here
// Don't edit the code below here!
const section = document.querySelector("section");
const para1 = document.createElement("p");
para1.textContent = finalQuote;
section.appendChild(para1);
點選此處顯示解決方案
你完成的 JavaScript 應該看起來像這樣
js
// ...
// Don't edit the code above here!
let fixedQuote = quote.toLowerCase();
const firstLetter = fixedQuote.slice(0, 1);
fixedQuote = fixedQuote.replace(firstLetter, firstLetter.toUpperCase());
fixedQuote = fixedQuote.replace("green eggs and ham", "pickled onions");
const finalQuote = `${fixedQuote}.`;
// Don't edit the code below here!
// ...
字串 4
在最後一個字串任務中,我們為您提供了一個定理的名稱、兩個數值和一個不完整的字串(需要新增的部分用星號 (*) 標記)。我們需要您更改字串的值。
完成任務
- 將字串從普通字串字面量更改為模板字面量。
- 用四個模板字面量嵌入表示式替換這四個星號。它們應該是:
- 定理的名稱。
- 我們擁有的兩個數值。
- 直角三角形斜邊的長度,假設另外兩條邊的長度與我們擁有的兩個數值相同。您需要查詢如何根據現有資訊計算它。在佔位符內進行計算。
js
const theorem = "Pythagorean theorem";
const a = 5;
const b = 8;
// Don't edit the code above here!
// Edit the string literal
const myString =
"Using *, we can work out that if the two shortest sides of a right-angled triangle have lengths of * and *, the length of the hypotenuse is *.";
// Don't edit the code below here!
const section = document.querySelector("section");
const para1 = document.createElement("p");
para1.textContent = myString;
section.appendChild(para1);
點選此處顯示解決方案
你完成的 JavaScript 應該看起來像這樣
js
// ...
// Don't edit the code above here!
const myString = `Using ${theorem}, we can work out that if the two shortest sides of a right-angled triangle have lengths of ${a} and ${b},
the length of the hypotenuse is ${Math.sqrt(a ** 2 + b ** 2)}.`;
// Don't edit the code below here!
// ...