2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244b8
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/cbQmmC4'
|
|
|
|
forumTopicId: 16803
|
2020-10-01 17:54:21 +02:00
|
|
|
title: 用 += 运算符连接字符串
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='description'>
|
|
|
|
我们还可以使用<code>+=</code>运算符来<dfn>concatenate</dfn>(拼接)字符串到现有字符串的结尾。对于那些被分割成几段的长的字符串来说,这一操作是非常有用的。
|
|
|
|
<strong>提示</strong><br>注意空格。拼接操作不会在两个字符串之间添加空格,所以如果想要加上空格的话,你需要自己在字符串里面添加。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
通过使用<code>+=</code>操作符来连接这两个字符串:<br><code>"This is the first sentence. "</code>和<code>"This is the second sentence."</code>并赋给变量<code>myStr</code>。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>myStr</code>的值应该是<code>This is the first sentence. This is the second sentence.</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(myStr === "This is the first sentence. This is the second sentence.");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: 使用<code>+=</code>操作符创建<code>myStr</code>变量。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(code.match(/\w\s*\+=\s*["']/g).length > 1 && code.match(/\w\s*\=\s*["']/g).length > 1);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Example
|
|
|
|
var ourStr = "I come first. ";
|
|
|
|
ourStr += "I come second.";
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
var myStr;
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
(function(){
|
|
|
|
if(typeof myStr === 'string') {
|
|
|
|
return 'myStr = "' + myStr + '"';
|
|
|
|
} else {
|
|
|
|
return 'myStr is not a string';
|
|
|
|
}
|
|
|
|
})();
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
var ourStr = "I come first. ";
|
|
|
|
ourStr += "I come second.";
|
|
|
|
|
|
|
|
var myStr = "This is the first sentence. ";
|
|
|
|
myStr += "This is the second sentence.";
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|