2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7b7e367417b2b2512b22
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 使用 parseInt 函数并传入一个基数
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/c6K4Kh3'
|
|
|
|
|
forumTopicId: 301182
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: use-the-parseint-function-with-a-radix
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
|
|
|
|
`parseInt()`函数解析一个字符串并返回一个整数。它同时可接受第二个参数,一个介于2和36之间的整数,表示字符串的基数。
|
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
函数调用如下所示:
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
`parseInt(string, radix);`
|
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
示例:
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
`var a = parseInt("11", 2);`
|
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
参数 2 表示 "11" 使用二进制数值系统。此示例将字符串 "11" 转换为整数 3。
|
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
|
|
|
|
在`convertToIntegerparseInt()`函数把二进制数转换成十进制并返回。
|
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
|
|
|
|
`convertToInteger`中应该使用`parseInt()`函数。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(/parseInt/g.test(code));
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`convertToInteger("10011")`应该返回一个数字。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(typeof convertToInteger('10011') === 'number');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`convertToInteger("10011")`应该返回 19。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(convertToInteger('10011') === 19);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`convertToInteger("111001")`应该返回 57。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(convertToInteger('111001') === 57);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`convertToInteger("JamesBond")`应该返回 NaN。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert.isNaN(convertToInteger('JamesBond'));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function convertToInteger(str) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
convertToInteger("10011");
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```js
|
|
|
|
|
function convertToInteger(str) {
|
|
|
|
|
return parseInt(str, 2);
|
|
|
|
|
}
|
|
|
|
|
```
|