freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-javascript/use-the-parseint-function.c...

1.9 KiB

id title challengeType videoUrl localeTitle
587d7b7e367417b2b2512b23 Use the parseInt Function 1 使用parseInt函数

Description

parseInt()函数解析字符串并返回一个整数。这是一个例子: var a = parseInt("007");上面的函数将字符串“007”转换为整数7.如果字符串中的第一个字符无法转换为数字,则返回NaN

Instructions

convertToInteger函数中使用parseInt() ,以便将输入字符串str转换为整数,然后返回它。

Tests

tests:
  - text: <code>convertToInteger</code>应该使用<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>应该返回一个数字
    testString: 'assert(typeof(convertToInteger("56")) === "number", "<code>convertToInteger("56")</code> should return a number");'
  - text: <code>convertToInteger(&quot;56&quot;)</code>应该返回56
    testString: 'assert(convertToInteger("56") === 56, "<code>convertToInteger("56")</code> should return 56");'
  - text: <code>convertToInteger(&quot;77&quot;)</code>应该返回77
    testString: 'assert(convertToInteger("77") === 77, "<code>convertToInteger("77")</code> should return 77");'
  - text: <code>convertToInteger(&quot;JamesBond&quot;)</code>应该返回NaN
    testString: 'assert.isNaN(convertToInteger("JamesBond"), "<code>convertToInteger("JamesBond")</code> should return NaN");'

Challenge Seed

function convertToInteger(str) {

}

convertToInteger("56");

Solution

// solution required