2018-10-12 15:37:13 -04:00
---
title: Use the parseInt Function with a Radix
---
2019-07-24 00:59:27 -07:00
# Use the parseInt Function with a Radix
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Hints
2018-10-24 10:40:49 +02:00
2019-07-24 00:59:27 -07:00
### Hint 1
2018-10-24 10:40:49 +02:00
If you use a variable to assign the result of `parseInt(str)` to it, remember to return that variable.
Otherwise you can just use a `return` statement for your function.
2019-07-24 00:59:27 -07:00
### Hint 2
2018-10-24 10:40:49 +02:00
In this exercise you need to "convert" a binary number into a decimal number using the `radix` parameter in order to specify the base on which the input number is represented on.
2019-07-24 00:59:27 -07:00
---
## Solutions
2018-10-24 10:40:49 +02:00
2019-07-24 00:59:27 -07:00
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
2018-10-24 10:40:49 +02:00
```javascript
function convertToInteger(str) {
return parseInt(str, 2);
}
convertToInteger("10011");
```
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-24 10:40:49 +02:00
- The function takes `str` and returns an integer instead of a string but "understanding" its a binary number instead of a decimal one thanks to the `radix` parameter (2).
2019-07-24 00:59:27 -07:00
#### Relevant Links
2018-10-24 10:40:49 +02:00
- ["parseInt()" - *MDN JavaScript reference* ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt )
- ["Radix" - *Wikipedia* ](https://en.wikipedia.org/wiki/Radix )
2019-07-24 00:59:27 -07:00
< / details >