2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244ed
|
2020-12-16 00:37:30 -07:00
|
|
|
title: 将变量附加到字符串
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/cbQmZfa'
|
|
|
|
forumTopicId: 16656
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: appending-variables-to-strings
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
我们不仅可以创建出多行的字符串,还可以使用加等号(`+=`)运算符来将变量追加到字符串。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
设置变量`someAdjective`的值,并使用`+=`运算符把它追加到变量`myStr`上。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`someAdjective`应该是一个至少包含三个字符的字符串。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(typeof someAdjective !== 'undefined' && someAdjective.length > 2);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
使用`+=`操作符把`someAdjective`追加到`myStr`的后面。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(code.match(/myStr\s*\+=\s*someAdjective\s*/).length > 0);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
```js
|
|
|
|
(function(){
|
|
|
|
var output = [];
|
|
|
|
if(typeof someAdjective === 'string') {
|
|
|
|
output.push('someAdjective = "' + someAdjective + '"');
|
|
|
|
} else {
|
|
|
|
output.push('someAdjective is not a string');
|
|
|
|
}
|
|
|
|
if(typeof myStr === 'string') {
|
|
|
|
output.push('myStr = "' + myStr + '"');
|
|
|
|
} else {
|
|
|
|
output.push('myStr is not a string');
|
|
|
|
}
|
|
|
|
return output.join('\n');
|
|
|
|
})();
|
|
|
|
```
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Change code below this line
|
|
|
|
|
|
|
|
var someAdjective;
|
|
|
|
var myStr = "Learning to code is ";
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```js
|
|
|
|
var someAdjective = "neat";
|
|
|
|
var myStr = "Learning to code is ";
|
|
|
|
myStr += someAdjective;
|
|
|
|
```
|