1.7 KiB
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--
我們還可以使用 +=
運算符來拼接字符串到現有字符串變量的結尾。 對於那些被分割成幾段的長的字符串來說,這一操作是非常有用的。
提示: 注意空格。 拼接操作不會在兩個字符串之間添加空格,所以,如果想要加上空格的話,你需要自己在字符串裏面添加。
例如:
var 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--
// Only change code below this line
var myStr;
--solutions--
var myStr = "This is the first sentence. ";
myStr += "This is the second sentence.";