FE-Interview icon indicating copy to clipboard operation
FE-Interview copied to clipboard

es5 实现 isInteger

Open lgwebdream opened this issue 4 years ago • 5 comments

lgwebdream avatar Jul 06 '20 15:07 lgwebdream

扫描下方二维码,获取答案以及详细解析,同时可解锁800+道前端面试题。

lgwebdream avatar Jul 06 '20 15:07 lgwebdream

Number.isInteger = function(val){ return typeof val === "number" && isfinite(val) && Math.floor(val) === value }

jeffcomtw avatar Aug 09 '20 06:08 jeffcomtw

Number.isInteger = function (val) {
  return typeof val === "number" && ~~val === val;
};

Xiaolong96 avatar Mar 23 '21 07:03 Xiaolong96

Number.isInteger = function (value) {
  // es5的形式
  return typeof value === 'number' &&
    Number.isFinite(value) &&
    Math.floor(value) === params;
}

zizxzy avatar Nov 08 '21 05:11 zizxzy

es5 实现 isInteger

Integer的特性?

  • 是number类型
  • 进行某些Math操作后得到的整数结果,与自身相等
function isInteger(num) {
  // Math.floor(num) === num
  // Math.ceil(num) === num
  // num % 1 === 0

  return typeof num === "number" && num % 1 === 0;
}

Kisthanny avatar Mar 22 '24 04:03 Kisthanny