字符串 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!
// ...