2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7b7e367417b2b2512b22
|
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/c6K4Kh3'
|
|
|
|
|
forumTopicId: 301182
|
|
|
|
|
localeTitle: 使用 parseInt 函数并传入一个基数
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
<code>parseInt()</code>函数解析一个字符串并返回一个整数。它同时可接受第二个参数,一个介于2和36之间的整数,表示字符串的基数。
|
|
|
|
|
函数调用如下所示:
|
|
|
|
|
<code>parseInt(string, radix);</code>
|
|
|
|
|
示例:
|
|
|
|
|
<code>var a = parseInt("11", 2);</code>
|
|
|
|
|
参数 2 表示 "11" 使用二进制数值系统。此示例将字符串 "11" 转换为整数 3。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
在<code>convertToInteger</code中使用<code>parseInt()</code>函数把二进制数转换成十进制并返回。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>convertToInteger</code>中应该使用<code>parseInt()</code>函数。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(/parseInt/g.test(code));
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>convertToInteger("10011")</code>应该返回一个数字。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(typeof(convertToInteger("10011")) === "number");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>convertToInteger("10011")</code>应该返回 19。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(convertToInteger("10011") === 19);
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>convertToInteger("111001")</code>应该返回 57。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(convertToInteger("111001") === 57);
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>convertToInteger("JamesBond")</code>应该返回 NaN。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert.isNaN(convertToInteger("JamesBond"));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function convertToInteger(str) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
convertToInteger("10011");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
|
function convertToInteger(str) {
|
|
|
|
|
return parseInt(str, 2);
|
|
|
|
|
}
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</section>
|