2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244b7
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/cNpM8AN'
|
|
|
|
forumTopicId: 16802
|
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'>
|
|
|
|
在 JavaScript 中,当对一个<code>String</code>类型的值使用<code>+</code>操作符的时候,它被称作 <dfn>拼接操作符</dfn>。你可以通过<dfn>拼接</dfn>其他字符串来创建一个新的字符串。
|
|
|
|
<strong>示例</strong>
|
|
|
|
|
|
|
|
```js
|
|
|
|
'My name is Alan,' + ' I concatenate.'
|
|
|
|
```
|
|
|
|
|
|
|
|
<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>操作符,把字符串<code>"This is the start. "</code>和<code>"This is the end."</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 start. This is the end.</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(myStr === "This is the start. This is the end.");
|
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(/(["']).*(["'])\s*\+\s*(["']).*(["'])/g).length > 1);
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>myStr</code>应该被<code>var</code>关键字声明。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(/var\s+myStr/.test(code));
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: 确保有给<code>myStr</code>赋值。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(/myStr\s*=/.test(code));
|
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. " + "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. " + "I come second.";
|
|
|
|
var myStr = "This is the start. " + "This is the end.";
|
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>
|