2018-10-04 14:37:37 +01:00
---
id: 587d7b7e367417b2b2512b23
title: Use the parseInt Function
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cm83LSW'
2019-08-05 09:17:33 -07:00
forumTopicId: 301183
2021-01-13 03:31:00 +01:00
dashedName: use-the-parseint-function
2018-10-04 14:37:37 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
The `parseInt()` function parses a string and returns an integer. Here's an example:
`var a = parseInt("007");`
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` .
# --instructions--
Use `parseInt()` in the `convertToInteger` function so it converts the input string `str` into an integer, and returns it.
# --hints--
`convertToInteger` should use the `parseInt()` function
```js
assert(/parseInt/g.test(code));
2018-10-04 14:37:37 +01:00
```
2020-11-27 19:02:05 +01:00
`convertToInteger("56")` should return a number
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof convertToInteger('56') === 'number');
```
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
`convertToInteger("56")` should return 56
2018-10-04 14:37:37 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(convertToInteger('56') === 56);
```
2018-10-08 01:01:53 +01:00
2020-11-27 19:02:05 +01:00
`convertToInteger("77")` should return 77
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(convertToInteger('77') === 77);
2018-10-04 14:37:37 +01:00
```
2020-11-27 19:02:05 +01:00
`convertToInteger("JamesBond")` should return NaN
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.isNaN(convertToInteger('JamesBond'));
```
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
function convertToInteger(str) {
}
convertToInteger("56");
```
# --solutions--
2018-10-04 14:37:37 +01:00
```js
2018-10-16 05:08:37 +05:30
function convertToInteger(str) {
return parseInt(str);
}
2018-10-04 14:37:37 +01:00
```