2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: acda2fb1324d9b0fa741e6b5
|
2021-03-13 17:31:57 +00:00
|
|
|
title: 确认结尾
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 5
|
2021-01-12 16:18:51 +00:00
|
|
|
forumTopicId: 16006
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: confirm-the-ending
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
检查字符串(第一个参数 `str`)是否以给定的目标字符串(第二个参数 `target`)结束。
|
2021-01-12 16:18:51 +00:00
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
这个挑战 *可以* 用 ES2015 引入的 `.endsWith()` 方法来解决。但在这个挑战中,请使用 JavaScript 的字符串子串方法。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`confirmEnding("Bastian", "n")` 应返回 `true`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(confirmEnding('Bastian', 'n') === true);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`confirmEnding("Congratulation", "on")` 应返回 `true`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(confirmEnding('Congratulation', 'on') === true);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`confirmEnding("Connor", "n")` 应返回 `false`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(confirmEnding('Connor', 'n') === false);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification")` 应返回 `false`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
confirmEnding(
|
|
|
|
'Walking on water and developing software from a specification are easy if both are frozen',
|
|
|
|
'specification'
|
|
|
|
) === false
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`confirmEnding("He has to give me a new name", "name")` 应返回 `true`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(confirmEnding('He has to give me a new name', 'name') === true);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`confirmEnding("Open sesame", "same")` 应返回 `true`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(confirmEnding('Open sesame', 'same') === true);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`confirmEnding("Open sesame", "sage")` 应返回 `false`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2021-01-12 16:18:51 +00:00
|
|
|
assert(confirmEnding('Open sesame', 'sage') === false);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`confirmEnding("Open sesame", "game")` 应返回 `false`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(confirmEnding('Open sesame', 'game') === false);
|
|
|
|
```
|
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain")` 应返回 `false`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
confirmEnding(
|
|
|
|
'If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing',
|
|
|
|
'mountain'
|
|
|
|
) === false
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`confirmEnding("Abstraction", "action")` 应该返回 `true`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(confirmEnding('Abstraction', 'action') === true);
|
|
|
|
```
|
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
不应使用内置方法 `.endsWith()` 来完成挑战。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(!/\.endsWith\(.*?\)\s*?;?/.test(code) && !/\['endsWith'\]/.test(code));
|
|
|
|
```
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function confirmEnding(str, target) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
confirmEnding("Bastian", "n");
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
|
|
|
function confirmEnding(str, target) {
|
|
|
|
return str.substring(str.length - target.length) === target;
|
|
|
|
}
|
|
|
|
|
|
|
|
confirmEnding("Bastian", "n");
|
|
|
|
```
|