2018-10-10 18:03:03 -04:00
---
id: 587d7b7e367417b2b2512b22
2021-02-06 04:42:36 +00:00
title: Use the parseInt Function with a Radix
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--
2021-02-06 04:42:36 +00:00
The `parseInt()` function parses a string and returns an integer. It takes a second argument for the radix, which specifies the base of the number in the string. The radix can be an integer between 2 and 36.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
The function call looks like:
2020-12-16 00:37:30 -07:00
`parseInt(string, radix);`
2021-02-06 04:42:36 +00:00
And here's an example:
2020-12-16 00:37:30 -07:00
`var a = parseInt("11", 2);`
2021-02-06 04:42:36 +00:00
The radix variable says that "11" is in the binary system, or base 2. This example converts the string "11" to an integer 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
2021-02-06 04:42:36 +00:00
Use `parseInt()` in the `convertToInteger` function so it converts a binary number to an integer and returns it.
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
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("10011")` should return a number
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
```
2021-02-06 04:42:36 +00:00
`convertToInteger("10011")` should return 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
2021-02-06 04:42:36 +00:00
`convertToInteger("111001")` should return 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
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("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);
}
```