freeCodeCamp/curriculum/challenges/chinese-traditional/02-javascript-algorithms-an.../basic-javascript/concatenating-strings-with-...

1.7 KiB

id title challengeType videoUrl forumTopicId dashedName
56533eb9ac21ba0edf2244b8 用 += 運算符連接字符串 1 https://scrimba.com/c/cbQmmC4 16803 concatenating-strings-with-the-plus-equals-operator

--description--

我們還可以使用 += 運算符來拼接字符串到現有字符串變量的結尾。 對於那些被分割成幾段的長的字符串來說,這一操作是非常有用的。

提示: 注意空格。 拼接操作不會在兩個字符串之間添加空格,所以,如果想要加上空格的話,你需要自己在字符串裏面添加。

例如:

let ourStr = "I come first. ";
ourStr += "I come second.";

ourStr 的值爲字符串 I come first. I come second.

--instructions--

使用 += 操作符,多行合併字符串 This is the first sentence.This is the second sentence. ,並賦值給 myStr 。 參照示例中顯示的方式使用 += 操作符,並確保在兩個字符串之間包含一個空格。 先把第一個字符串賦值給 myStr,然後拼接第二個字符串。

--hints--

myStr 的值應該是字符串 This is the first sentence. This is the second sentence.

assert(myStr === 'This is the first sentence. This is the second sentence.');

應該使用 += 操作符創建 myStr 變量。

assert(code.match(/myStr\s*\+=\s*(["']).*\1/g));

--seed--

--after-user-code--

(function(){
  if(typeof myStr === 'string') {
    return 'myStr = "' + myStr + '"';
  } else {
    return 'myStr is not a string';
  }
})();

--seed-contents--

let myStr;

--solutions--

let myStr = "This is the first sentence. ";
myStr += "This is the second sentence.";