2018-10-10 18:03:03 -04:00
---
id: 587d7b7e367417b2b2512b22
title: Use the parseInt Function with a Radix
challengeType: 1
2019-08-28 16:26:13 +03:00
videoUrl: https://scrimba.com/c/c6K4Kh3
forumTopicId: 301182
2018-10-10 18:03:03 -04:00
localeTitle: Используйте функцию parseInt с помощью Radix
---
## Description
2019-08-28 16:26:13 +03:00
< section id = 'description' >
Функция < code > parseInt()< / code > анализирует строку и возвращает целое число. Он принимает второй аргумент для radix, который определяет базу номера в строке. Радикс может быть целым числом от 2 до 36. Вызов функции выглядит так: < code > parseInt(string, radix);< / code > И вот пример: < code > var a = parseInt(" 11" , 2);< / code > В переменной radix указано, что «11» находится в двоичной системе или базе 2. Этот пример преобразует строку «11» в целое число 3.
< / section >
2018-10-10 18:03:03 -04:00
## Instructions
2019-08-28 16:26:13 +03:00
< section id = 'instructions' >
Используйте функцию < code > parseInt()< / code > в функции < code > convertToInteger< / code > чтобы она преобразует двоичное число в целое и возвращает е г о .
< / section >
2018-10-10 18:03:03 -04:00
## Tests
< section id = 'tests' >
```yml
tests:
2019-08-28 16:26:13 +03:00
- text: < code > convertToInteger</ code > should use the < code > parseInt()</ code > function
testString: assert(/parseInt/g.test(code));
- text: < code > convertToInteger("10011")</ code > should return a number
testString: assert(typeof(convertToInteger("10011")) === "number");
- text: < code > convertToInteger("10011")</ code > should return 19
testString: assert(convertToInteger("10011") === 19);
- text: < code > convertToInteger("111001")</ code > should return 57
testString: assert(convertToInteger("111001") === 57);
- text: < code > convertToInteger("JamesBond")</ code > should return NaN
testString: assert.isNaN(convertToInteger("JamesBond"));
2018-10-10 18:03:03 -04:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function convertToInteger(str) {
}
convertToInteger("10011");
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-08-28 16:26:13 +03:00
function convertToInteger(str) {
return parseInt(str, 2);
}
2018-10-10 18:03:03 -04:00
```
2019-08-28 16:26:13 +03:00
2018-10-10 18:03:03 -04:00
< / section >