freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-an.../basic-javascript/use-the-parseint-function-w...

1.9 KiB

id title challengeType videoUrl forumTopicId
587d7b7e367417b2b2512b22 Use the parseInt Function with a Radix 1 https://scrimba.com/c/c6K4Kh3 301182

Description

The parseInt() function parses a string and returns an integer. It takes a second argument for the radix, which specifies the base of the number in the string. The radix can be an integer between 2 and 36. The function call looks like: parseInt(string, radix); And here's an example: var a = parseInt("11", 2); The radix variable says that "11" is in the binary system, or base 2. This example converts the string "11" to an integer 3.

Instructions

Use parseInt() in the convertToInteger function so it converts a binary number to an integer and returns it.

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);
}