2018-10-10 18:03:03 -04:00
---
id: 587d7b7e367417b2b2512b23
2021-02-06 04:42:36 +00:00
title: Use the parseInt Function
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/cm83LSW'
forumTopicId: 301183
2021-01-13 03:31:00 +01:00
dashedName: use-the-parseint-function
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
2021-02-06 04:42:36 +00:00
The `parseInt()` function parses a string and returns an integer. Here's an example:
2020-12-16 00:37:30 -07:00
`var a = parseInt("007");`
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The above function converts the string "007" to an integer 7. If the first character in the string can't be converted into a number, then it returns `NaN` .
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
2021-02-06 04:42:36 +00:00
Use `parseInt()` in the `convertToInteger` function so it converts the input string `str` into an integer, and returns it.
2020-12-16 00:37:30 -07:00
# --hints--
2021-02-06 04:42:36 +00:00
`convertToInteger` should use the `parseInt()` function
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
2021-02-06 04:42:36 +00:00
`convertToInteger("56")` should return a number
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(typeof convertToInteger('56') === 'number');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`convertToInteger("56")` should return 56
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(convertToInteger('56') === 56);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`convertToInteger("77")` should return 77
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(convertToInteger('77') === 77);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`convertToInteger("JamesBond")` should return 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("56");
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
function convertToInteger(str) {
return parseInt(str);
}
```