2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: bd7123c9c451eddfaeb5bdef
|
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/cBZQGcv'
|
|
|
|
forumTopicId: 18342
|
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
|
|
|
要获取字符串的最后一个字符,可以用字符串的长度减 1 的索引值。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
例如,在`var firstName = "Charles"`中,你可以这样操作`firstName[firstName.length - 1]`来得到字符串的最后的一个字符。
|
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
|
|
|
使用<dfn>方括号<dfn来取得<code>lastName变量中的最后一个字符。 <strong>提示</strong><br>如果你遇到困难了,不妨看看在<code>lastLetterOfFirstName</code>变量上是怎么做的。 </dfn来取得<code></code></code></dfn>
|
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
|
|
|
`lastLetterOfLastName`应该是"e"。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(lastLetterOfLastName === 'e');
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
你需要使用`.length`获取最后一个字符。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(code.match(/\.length/g).length === 2);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
2020-04-29 18:29:13 +08:00
|
|
|
|