简体   繁体   中英

Modify the function to return true if the given number is an integer, and false otherwise

I am not sure what exactly this question is asking for. Just feeling confused. Please help!

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');

when you console log you should get true for the whole number and false for the decimal.

Look at How to check if a variable is an integer in JavaScript? to figure out the logic behind checking if the input is an integer. The simple function I create below returns true if the given number is an integer and false if not.

 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM