JS Pen question parseint () and. Map ()

Source: Internet
Author: User

A few examples of parseint ()

var B = parseint ("b="), Alert ("+b"), var C = parseint ("09/08/2009"); Alert ("c=" +c);

Results: b=1,c=0

parseint () is used to parse a string and return an integer with several features:

1. Ignore the first and last whitespace of the parsed string, that is: "111" and "111" are the same

2. The formal format is parseint (String,radix).

The first argument is the parsed string, and the second argument is the binary (which is not a bit)

The second parameter features:

① if Radix is omitted or 0, it is parsed by default decimal

②radix is an integer between 2~36 and, if outside this range, returns Nan

The first character of the ③string is 0, and the default resolution is 8 binary. A string with a first character of 0x (0X) is parsed by default to 16 binary. In other cases, the default is 10 binary.

④ better Add the second parameter, or even if the first character is 0, it should be in 8 binary mode, in some browsers, such as FF will also be 10 binary

3. Parsing begins with the first resolvable character, ending with the first unresolved character, such as a space, punctuation, and so on. Subsequent characters are no longer resolved. If the first character cannot be parsed, a nan is returned.

Back to the question to analyze

The first letter of B is 0, according to 8, the following value is parsed, which is 8 binary 1. The natural return value is 1.
The first letter of C is 0, according to 8 to parse the following values, but the second character 9 is not 8 binary number, that is, 9 is the first unresolved character, parseint ("09/08/2009") equivalent to parseint ("0"), the natural return value is 0
Note: 8 is also not a 8 binary number, Max to 7

Deformation:

var c = parseint ("0119/08/2009"); Alert ("c=" +c);

Analysis:

Equivalent to parseint ("011"), 0*8^2+1*8^1+1*8^0=0+8+1=9, output is c=9

Parsefloat () and parseint ()

parseint () can take parameters, specify binary, octal, or hexadecimal (the default decimal), and parsefloat () without parameters, cannot parse octal, hexadecimal number, only in decimal.

Take a look at examples of parseint () and. Map () combinations

What is the return value of ["1", "2", "3"].map (parseint)?

Map method

array1.map(callbackfn[, thisArg])

Each element of an array, called the definition of a callback function, returns a list containing the result

Parameters:

Array1: Must. An Array object

CALLBACKFN: Must. A callback function that accepts up to three parameters (the value of the array element, the index of the array element, the array object containing the element)

Thisarg: Optional.

The original example is equivalent to

[Parseint ("1", 0), parseint ("2", 1), parseint ("3", 2)]

Analysis:

1 by decimal conversion Output 11 is not between 2~36, Output nan string "3" There is no valid binary number, output Nan so the final result is [1,nan,nan]

JS Pen question parseint () and. Map ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.