JavaScript encoding specification-correct use of parseint

Source: Internet
Author: User

The topic describes how to modify the invocation of parseint in the JS code so that it is entered through all test case Example 1
' 12 '
Output
12
Example 2 input replication
' 12px '
Output replication
12
Example 3 input
' 0x12 '
Output
0

function Parse2int (num) {    var type = typeof num;    if (type = = = "Number") {        if (num% 1 = = = 0) {            return num;        } else {            return num-num%1;}    } else if ( Type = = = "string") {        var s = "", N0 = "0". charCodeAt (0), N9 = "9". charCodeAt (0);        for (var i = 0; i < num.length; i++) {            var c = num.charcodeat (i);            if (N0 <= c && C <= N9) {                s + = Num.charat (i);            } else {break                ;            }        }        function S2i (s) {            var n = 0, n0 = "0". charCodeAt (0);            for (var i = 0; i < s.length; i++) {                n = 10*n + (s.charcodeat (i)-n0);            }            return n;        }        return s2i (s);    }    return NaN;}

  

* Process Control

To implement the Fizzbuzz function, the relationship between the parameter num and the return value is as follows:
1. If num can be divisible by 3 and 5 at the same time, return the string Fizzbuzz
2. If num can be divisible by 3, return the string fizz
3. If num can be divisible by 5, return string buzz
4. Returns False if the argument is empty or is not of type number
5, the rest of the case, the return parameter num

Input:15 Output:fizzbuzz

function Fizzbuzz (num) {    if (!num) {return! 1;}    if (typeof num = = = "Number") {        var a = num%3===0, B = num%5==0;        Return a&&b? "Fizzbuzz": A? "Fizz": B? "Buzz": num;    }    return! 1;}

  

JavaScript encoding specification-correct use of parseint

Related Article

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.