2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244b9
|
2021-03-14 21:20:39 -06: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/cqk8rf4'
|
|
|
|
forumTopicId: 16805
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: constructing-strings-with-variables
|
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
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
有时候你需要创建一个类似 [Mad Libs](https://en.wikipedia.org/wiki/Mad_Libs)(填词游戏)风格的字符串。 通过使用连接运算符(`+`),你可以插入一个或多个变量来组成一个字符串。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
例如:
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
```js
|
2021-10-27 15:10:57 +00:00
|
|
|
const ourName = "freeCodeCamp";
|
|
|
|
const ourStr = "Hello, our name is " + ourName + ", how are you?";
|
2021-02-06 04:42:36 +00:00
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`ourStr` 值为 `Hello, our name is freeCodeCamp, how are you?`
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
把你的名字赋值给变量 `myName`,然后把变量 `myName` 插入到字符串 `My name is` 和 `and I am well!` 之间,并把连接后的结果赋值给变量 `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
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`myName` 应该是一个至少有 3 个字符的字符串。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(typeof myName !== 'undefined' && myName.length > 2);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
使用两个 `+` 操作符创建包含 `myName` 的 `myStr` 变量。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(code.match(/["']\s*\+\s*myName\s*\+\s*["']/g).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 myName === 'string') {
|
|
|
|
output.push('myName = "' + myName + '"');
|
|
|
|
} else {
|
|
|
|
output.push('myName 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
|
|
|
|
// Only change code below this line
|
2021-10-27 15:10:57 +00:00
|
|
|
const myName = "";
|
|
|
|
const myStr = "";
|
2021-01-13 03:31:00 +01:00
|
|
|
```
|
|
|
|
|
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
|
2021-10-27 15:10:57 +00:00
|
|
|
const myName = "Bob";
|
|
|
|
const myStr = "My name is " + myName + " and I am well!";
|
2021-01-13 03:31:00 +01:00
|
|
|
```
|