2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: 587d7daa367417b2b2512b6c
|
2021-03-15 20:01:16 +00:00
|
|
|
title: 使用 join 方法将数组组合成字符串
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 1
|
2020-08-05 08:38:04 +00:00
|
|
|
forumTopicId: 18221
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: combine-an-array-into-a-string-using-the-join-method
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-03-15 20:01:16 +00:00
|
|
|
`join` 方法用来把数组中的所有元素放入一个字符串。 并通过指定的分隔符参数进行分隔。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-15 20:01:16 +00:00
|
|
|
举个例子:
|
2020-08-05 08:38:04 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
var arr = ["Hello", "World"];
|
|
|
|
var str = arr.join(" ");
|
|
|
|
```
|
|
|
|
|
2021-03-15 20:01:16 +00:00
|
|
|
`str` 的值应该是字符串 `Hello World`。
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 20:01:16 +00:00
|
|
|
在函数 `sentensify` 内用 `join` 方法(及其他方法)用字符串 `str` 中的单词造句,这个函数应返回一个字符串。 该函数应返回一个数组。 举个例子,`I-like-Star-Wars` 会被转换成 `I like Star Wars`。 在此挑战中请勿使用 `replace` 方法。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 20:01:16 +00:00
|
|
|
应使用 `join` 方法。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(code.match(/\.join/g));
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 20:01:16 +00:00
|
|
|
不能使用 `replace` 方法。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
assert(!code.match(/\.?[\s\S]*?replace/g));
|
2020-12-16 07:37:30 +00:00
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 20:01:16 +00:00
|
|
|
`sentensify("May-the-force-be-with-you")` 应返回一个字符串。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(typeof sentensify('May-the-force-be-with-you') === 'string');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 20:01:16 +00:00
|
|
|
`sentensify("May-the-force-be-with-you")` 应返回 `May the force be with you`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(sentensify('May-the-force-be-with-you') === 'May the force be with you');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 20:01:16 +00:00
|
|
|
`sentensify("The.force.is.strong.with.this.one")` 应返回 `The force is strong with this one`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
sentensify('The.force.is.strong.with.this.one') ===
|
|
|
|
'The force is strong with this one'
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 20:01:16 +00:00
|
|
|
`sentensify("There,has,been,an,awakening")` 应返回 `There has been an awakening`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(
|
|
|
|
sentensify('There,has,been,an,awakening') === 'There has been an awakening'
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2020-08-05 08:38:04 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function sentensify(str) {
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
}
|
|
|
|
sentensify("May-the-force-be-with-you");
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
|
|
|
function sentensify(str) {
|
|
|
|
// Only change code below this line
|
|
|
|
return str.split(/\W/).join(' ');
|
|
|
|
// Only change code above this line
|
|
|
|
}
|
|
|
|
```
|