[Algorithm] with autistic javaScript is an integer determined whether a method (five kinds)

A mode using operator judgment Remainder

Ideas: the use of any integer 1 will be divisible, which is specific to the remainder 0 by the rules to determine whether or not an integer.

function isInteger(obj) {
 return obj%1 === 0
}
isInteger(5) // true
isInteger(5.72) // false 

As can be seen above the output of this function very easy to use, but for some special values ​​and string [] about the need to improve the inspection rules, otherwise it will be "cheating" for an empty string, string type numbers, Boolean true, empty returns the array are true, then modulo time because they are installed for the type, the empty string as follows, empty array are converted to 0, and it will turn into a true

isInteger('') // true
isInteger('5') // true
isInteger(true) // true
isInteger([]) // true

So before we determine what types to compare and optimize follows:

function isInteger(obj) {
 return typeof obj === 'number' && obj%1 === 0
}
isInteger('') // false
isInteger('5') // false
isInteger(true) // false
isInteger([]) // false

Second way, using Math.round, Math.ceil, Math.floor Analyzing

Idea: after rounding integer equal to or own. Use this feature to determine whether it is an integer Math.floorof example, as follows:

unction isInteger(obj) {
 return Math.floor(obj) === obj
}
isInteger(5) // true
isInteger(5.72) // false
isInteger('') // false
isInteger('5') // false
isInteger(true) // false
isInteger([]) // false

Math.floorThe method of automatically proceeds to the parameter into digital values, using the same token Math.ceilrounding up, Math.roundrounding

Three ways, by the parseInt () Analyzing

Ideas: use parseInt () decimal conversion features

function isInteger(obj) {
 return parseInt(obj, 10) === obj
}
isInteger(5) // true
isInteger(5.72) // false
isInteger('') // false
isInteger('5.72') // false
isInteger(true) // false
isInteger([]) // false

But for more than the number of detection range of detection will not, return to the first digit of the unified digital detection

parseInt(1000000000000000000000) // 1  false
parseInt(412300213123400000000.21312) // 4  false

Four ways, through the bit operation "|" judge

Thinking: bitwise OR operator |are binary operators. After two numbers whose function is involved in computing converted into 32-bit binary, each corresponding phase or binary. As long as the corresponding two binary bit is a 1, it is a result bit. Thus the use of this feature, any number and is 0 or return its own, for the same reason non-numeric type are the bits after conversion or

function isInteger(obj) {
 return (obj | 0) === obj
}
isInteger(5) // true
isInteger(5.72) // false
isInteger('') // false
isInteger('5') // false
isInteger(true) // false
isInteger([]) // false

OR operation on bits, than efficiency Math.floor, parseInthigher, bit For practical method operator can look → Javascript bitwise operators in use scenario + + + Package Performance Comparison

Way five, ES6 provided Number.isInteger

Idea: This is a method Number of API, is used to determine whether a given integer parameters specific reference → Number.isInteger

Number.isInteger(5) // true
Number.isInteger(5.72) // false
Number.isInteger('') // false
Number.isInteger('5') // false
Number.isInteger(true) // false
Number.isInteger([]) // false

Compatible with most browsers have implemented support, the IE does not support
Here Insert Picture Description

Epilogue

These are five ways to determine whether the integer type, this five ways have advantages and disadvantages, we can carefully compare and choose the best use.

Published 134 original articles · won praise 80 · views 30000 +

Guess you like

Origin blog.csdn.net/Umbrella_Um/article/details/103441512