2021-05-05 10:13:49 -07:00
|
|
|
---
|
|
|
|
id: bd7123c9c448eddfaeb5bdef
|
|
|
|
title: 查找字符串的長度
|
|
|
|
challengeType: 1
|
|
|
|
videoUrl: 'https://scrimba.com/c/cvmqEAd'
|
|
|
|
forumTopicId: 18182
|
|
|
|
dashedName: find-the-length-of-a-string
|
|
|
|
---
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
|
|
|
你可以通過在字符串變量或字符串後面寫上 `.length` 來獲得 `String` 的長度。
|
|
|
|
|
|
|
|
```js
|
|
|
|
console.log("Alan Peter".length);
|
|
|
|
```
|
|
|
|
|
|
|
|
字符串 `10` 將會出現在控制檯中。
|
|
|
|
|
2021-11-06 08:56:52 -07:00
|
|
|
例如,如果我們創建了一個變量 `const firstName = "Ada"`,我們可以通過使用 `firstName.length` 找出字符串 `Ada` 的長度屬性。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
|
|
使用 `.length` 屬性來獲得變量 `lastName` 的長度,並把它賦值給變量 `lastNameLength`。
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
|
|
不能改變 `// Setup` 部分聲明的變量。
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
2021-11-06 08:56:52 -07:00
|
|
|
code.match(/let lastNameLength = 0;/) &&
|
|
|
|
code.match(/const lastName = "Lovelace";/)
|
2021-05-05 10:13:49 -07:00
|
|
|
);
|
|
|
|
```
|
|
|
|
|
|
|
|
`lastNameLength` 應該等於 8。
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(typeof lastNameLength !== 'undefined' && lastNameLength === 8);
|
|
|
|
```
|
|
|
|
|
|
|
|
你應該使用 `.length` 獲取 `lastName` 的長度,像這樣 `lastName.length`。
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(code.match(/=\s*lastName\.length/g) && !code.match(/lastName\s*=\s*8/));
|
|
|
|
```
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Setup
|
2021-11-06 08:56:52 -07:00
|
|
|
let lastNameLength = 0;
|
|
|
|
const lastName = "Lovelace";
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
lastNameLength = lastName;
|
|
|
|
```
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
```js
|
2021-11-06 08:56:52 -07:00
|
|
|
let lastNameLength = 0;
|
|
|
|
const lastName = "Lovelace";
|
2021-05-05 10:13:49 -07:00
|
|
|
lastNameLength = lastName.length;
|
|
|
|
```
|