如果给定数字是整数,则修改函数以返回true,否则返回false

时间:2019-07-14 00:12:19

标签: javascript xcode numbers integer

我不确定这个问题到底在问什么。只是感到困惑。请帮忙!

function isInteger(num) {

}

/* Do not modify code below this line */

console.log(isInteger(1), '<-- should be true');
console.log(isInteger(1.5), '<-- should be false');

在控制台日志中,您应该对整数取真,对小数取假。

1 个答案:

答案 0 :(得分:0)

请看How to check if a variable is an integer in JavaScript?找出检查输入是否为整数的逻辑。如果给定数字是整数,则我在下面创建的简单函数返回true,否则返回false。

function isInteger(num) {
  if(num === parseInt(num, 10)){ // checks if input is integer
    return true;
  } else {
    return false;
  }
}

/* Do not modify code below this line */

console.log(isInteger(1), '<-- should be true');
console.log(isInteger(1.5), '<-- should be false');