2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7b7e367417b2b2512b23
|
|
|
|
title: Use the parseInt Function
|
|
|
|
challengeType: 1
|
|
|
|
videoUrl: ''
|
|
|
|
localeTitle: 使用parseInt函数
|
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
|
|
|
<section id="description"> <code>parseInt()</code>函数解析字符串并返回一个整数。这是一个例子: <code>var a = parseInt("007");</code>上面的函数将字符串“007”转换为整数7.如果字符串中的第一个字符无法转换为数字,则返回<code>NaN</code> 。 </section>
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
<section id="instructions">在<code>convertToInteger</code>函数中使用<code>parseInt()</code> ,以便将输入字符串<code>str</code>转换为整数,然后返回它。 </section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
|
|
|
- text: <code>convertToInteger</code>应该使用<code>parseInt()</code>函数
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(/parseInt/g.test(code));
|
2018-10-10 18:03:03 -04:00
|
|
|
- text: <code>convertToInteger("56")</code>应该返回一个数字
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(typeof(convertToInteger("56")) === "number");
|
2018-10-10 18:03:03 -04:00
|
|
|
- text: <code>convertToInteger("56")</code>应该返回56
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(convertToInteger("56") === 56);
|
2018-10-10 18:03:03 -04:00
|
|
|
- text: <code>convertToInteger("77")</code>应该返回77
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(convertToInteger("77") === 77);
|
2018-10-10 18:03:03 -04: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("56");
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
// solution required
|
|
|
|
```
|
|
|
|
</section>
|