2020-10-06 23:10:08 +05:30

1.7 KiB

id, challengeType, videoUrl, forumTopicId, localeTitle
id challengeType videoUrl forumTopicId localeTitle
587d7b7e367417b2b2512b23 1 https://scrimba.com/c/cm83LSW 301183 使用 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));
  - text: <code>convertToInteger("56")</code>应该返回一个数字。
    testString: assert(typeof(convertToInteger("56")) === "number");
  - text: <code>convertToInteger("56")</code>应该返回 56。
    testString: assert(convertToInteger("56") === 56);
  - text: <code>convertToInteger("77")</code>应该返回 77。
    testString: assert(convertToInteger("77") === 77);
  - text: <code>convertToInteger("JamesBond")</code>应该返回 NaN。
    testString: assert.isNaN(convertToInteger("JamesBond"));

Challenge Seed

function convertToInteger(str) {

}

convertToInteger("56");

Solution

function convertToInteger(str) {
  return parseInt(str);
}