2018-10-10 18:03:03 -04:00
---
id: 587d7b7e367417b2b2512b23
title: Use the parseInt Function
challengeType: 1
2019-08-28 16:26:13 +03:00
videoUrl: https://scrimba.com/c/cm83LSW
forumTopicId: 301183
2018-10-10 18:03:03 -04:00
localeTitle: Используйте функцию parseInt
---
## Description
2019-08-28 16:26:13 +03:00
< section id = 'description' >
Функция < code > parseInt()< / code > анализирует строку и возвращает целое число. Вот пример: < code > var a = parseInt(" 007" );< / code > Вышеупомянутая функция преобразует строку «007» в целое число 7. Если первый символ в строке не может быть преобразован в число, то он возвращает < code > NaN< / code > .
< / 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 > чтобы она преобразует входную строку < code > str< / 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("56")</ code > should return a number
testString: assert(typeof(convertToInteger("56")) === "number");
- text: < code > convertToInteger("56")</ code > should return 56
testString: assert(convertToInteger("56") === 56);
- text: < code > convertToInteger("77")</ code > should return 77
testString: assert(convertToInteger("77") === 77);
- 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("56");
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-08-28 16:26:13 +03:00
function convertToInteger(str) {
return parseInt(str);
}
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 >