JSLint says “missing radix parameter”

前端 未结 11 1266
北荒
北荒 2020-11-30 17:10

I ran JSLint on this JavaScript code and it said:

Problem at line 32 character 30: Missing radix parameter.

This is the code i

相关标签:
11条回答
  • 2020-11-30 17:38

    I solved it with just using the +foo, to convert the string.

    Keep in mind it's not great for readability (dirty fix).

    console.log( +'1' )
    // 1 (int)
    
    0 讨论(0)
  • 2020-11-30 17:39

    Just put an empty string in the radix place, because parseInt() take two arguments:

    parseInt(string, radix);

    string The value to parse. If the string argument is not a string, then it is converted to a string (using the ToString abstract operation). Leading whitespace in the string argument is ignored.

    radix An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above-mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.

    imageIndex = parseInt(id.substring(id.length - 1))-1;
    imageIndex = parseInt(id.substring(id.length - 1), '')-1;

    0 讨论(0)
  • 2020-11-30 17:44

    You can also simply add this line right above your parseInt line:

    // eslint-disable-next-line
    

    This will disable eslint check for the next line. Use this if you only need to skip one or two lines.

    0 讨论(0)
  • 2020-11-30 17:45

    I'm not properly answering the question but, I think it makes sense to clear why we should specify the radix.

    On MDN documentation we can read that:

    If radix is undefined or 0 (or absent), JavaScript assumes the following:

    • [...]
    • If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
    • [...]

    Source: MDN parseInt()

    0 讨论(0)
  • 2020-11-30 17:46

    It always a good practice to pass radix with parseInt -

    parseInt(string, radix)
    

    For decimal -

    parseInt(id.substring(id.length - 1), 10)
    

    If the radix parameter is omitted, JavaScript assumes the following:

    • If the string begins with "0x", the radix is 16 (hexadecimal)
    • If the string begins with "0", the radix is 8 (octal). This feature is deprecated
    • If the string begins with any other value, the radix is 10 (decimal)

    (Reference)

    0 讨论(0)
提交回复
热议问题