2021-06-15 07:49:18 +00:00
---
id: 587d7b7e367417b2b2512b22
2021-07-21 15:23:20 +00:00
title: Usar a função parseInt com um radix
2021-06-15 07:49:18 +00:00
challengeType: 1
videoUrl: 'https://scrimba.com/c/c6K4Kh3'
forumTopicId: 301182
dashedName: use-the-parseint-function-with-a-radix
---
# --description--
2021-07-14 15:32:51 +00:00
A função `parseInt()` analisa uma string e retorna um inteiro. É preciso um segundo argumento para o radix, que especifica a base do número na string. O radix pode ser um inteiro entre 2 e 36.
2021-06-15 07:49:18 +00:00
2021-07-14 15:32:51 +00:00
A chamada da função se parece com:
2021-06-15 07:49:18 +00:00
```js
parseInt(string, radix);
```
2021-07-14 15:32:51 +00:00
E aqui um exemplo:
2021-06-15 07:49:18 +00:00
```js
var a = parseInt("11", 2);
```
2021-07-14 15:32:51 +00:00
A variável radix diz que `11` está no sistema binário, ou base 2. Esse exemplo converte a string `11` para um inteiro `3` .
2021-06-15 07:49:18 +00:00
# --instructions--
2021-07-14 15:32:51 +00:00
Use `parseInt()` na função `convertToInteger` para que ela converta um número binário em um inteiro e o retorne.
2021-06-15 07:49:18 +00:00
# --hints--
2021-07-14 15:32:51 +00:00
`convertToInteger` deve usar a função `parseInt()`
2021-06-15 07:49:18 +00:00
```js
assert(/parseInt/g.test(code));
```
2021-07-14 15:32:51 +00:00
`convertToInteger("10011")` deve retornar um número
2021-06-15 07:49:18 +00:00
```js
assert(typeof convertToInteger('10011') === 'number');
```
2021-07-14 15:32:51 +00:00
`convertToInteger("10011")` deve retornar 19
2021-06-15 07:49:18 +00:00
```js
assert(convertToInteger('10011') === 19);
```
2021-07-14 15:32:51 +00:00
`convertToInteger("111001")` deve retornar 57
2021-06-15 07:49:18 +00:00
```js
assert(convertToInteger('111001') === 57);
```
2021-07-14 15:32:51 +00:00
`convertToInteger("JamesBond")` deve retornar `NaN`
2021-06-15 07:49:18 +00:00
```js
assert.isNaN(convertToInteger('JamesBond'));
```
# --seed--
## --seed-contents--
```js
function convertToInteger(str) {
}
convertToInteger("10011");
```
# --solutions--
```js
function convertToInteger(str) {
return parseInt(str, 2);
}
```