freeCodeCamp/curriculum/challenges/portuguese/02-javascript-algorithms-an.../basic-javascript/use-the-parseint-function.p...

2.0 KiB

id title challengeType videoUrl localeTitle
587d7b7e367417b2b2512b23 Use the parseInt Function 1 Use a função parseInt

Description

A função parseInt() analisa uma string e retorna um inteiro. Aqui está um exemplo: var a = parseInt("007"); A função acima converte a string "007" em um inteiro 7. Se o primeiro caractere da string não puder ser convertido em um número, ele retornará NaN .

Instructions

Use parseInt() na função convertToInteger para converter a string de entrada str em um inteiro e retorná-lo.

Tests

tests:
  - text: <code>convertToInteger</code> deve usar a função <code>parseInt()</code>
    testString: 'assert(/parseInt/g.test(code), "<code>convertToInteger</code> should use the <code>parseInt()</code> function");'
  - text: <code>convertToInteger(&quot;56&quot;)</code> deve retornar um número
    testString: 'assert(typeof(convertToInteger("56")) === "number", "<code>convertToInteger("56")</code> should return a number");'
  - text: <code>convertToInteger(&quot;56&quot;)</code> deve retornar 56
    testString: 'assert(convertToInteger("56") === 56, "<code>convertToInteger("56")</code> should return 56");'
  - text: <code>convertToInteger(&quot;77&quot;)</code> deve retornar 77
    testString: 'assert(convertToInteger("77") === 77, "<code>convertToInteger("77")</code> should return 77");'
  - text: <code>convertToInteger(&quot;JamesBond&quot;)</code> deve retornar NaN
    testString: 'assert.isNaN(convertToInteger("JamesBond"), "<code>convertToInteger("JamesBond")</code> should return NaN");'

Challenge Seed

function convertToInteger(str) {

}

convertToInteger("56");

Solution

// solution required