2.3 KiB
2.3 KiB
id, title, challengeType, videoUrl, forumTopicId, localeTitle
id | title | challengeType | videoUrl | forumTopicId | localeTitle |
---|---|---|---|---|---|
587d7b7e367417b2b2512b22 | Use the parseInt Function with a Radix | 1 | https://scrimba.com/c/c6K4Kh3 | 301182 | Используйте функцию parseInt с помощью Radix |
Description
parseInt()
анализирует строку и возвращает целое число. Он принимает второй аргумент для radix, который определяет базу номера в строке. Радикс может быть целым числом от 2 до 36. Вызов функции выглядит так: parseInt(string, radix);
И вот пример: var a = parseInt("11", 2);
В переменной radix указано, что «11» находится в двоичной системе или базе 2. Этот пример преобразует строку «11» в целое число 3.
Instructions
parseInt()
в функции convertToInteger
чтобы она преобразует двоичное число в целое и возвращает его.
Tests
tests:
- 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"));
Challenge Seed
function convertToInteger(str) {
}
convertToInteger("10011");
Solution
function convertToInteger(str) {
return parseInt(str, 2);
}