0

I'm starting to learn JavaScript at school and one of the assignments require me to check user's input whether it is an Integer or not.

This code DOES NOT WORK FOR ME ON CHROME.

var person = prompt("Please enter your name", "Enter Name");
alert("Hello " + person);
var age = prompt("Please enter your age", "Enter age");

if (age == parseInt(age, 10))
    alert("data is integer")
else
    alert("data is not an integer")

Whether I enter a string or integer in my prompt box, it always display the "data is not an integer" message.

user3072143
  • 77
  • 1
  • 2
  • 7

3 Answers3

7

prompt will always return a string so in your case:

var integerAge = parseInt(age);

if(!isNaN(integerAge) && age === '' + integerAge)
    alert("data is integer")
else
    alert("data is not an integer")

In the case of an age, you'll also probably check it's a positive integer with some integerAge >= 0 or custom minimum and maximum in the next validation step.

KyleK
  • 4,643
  • 17
  • 33
  • 2
    == is not checking the type of the variable, it should have the same behavior – Hacketo Jan 28 '15 at 13:57
  • Yes I know, so I use === that check the type. – KyleK Jan 28 '15 at 13:58
  • 1
    Could you explain why the original code doesn't work? – JJJ Jan 28 '15 at 13:59
  • 1
    @user3072143 `"15" == 15` and `"15" === "" + 15` result with the same value , how could it solve your problem ? – Hacketo Jan 28 '15 at 14:02
  • @Hacketo "015" == 15 is different from "015" === "" + 15 It depend on what you exactly need. – KyleK Jan 28 '15 at 14:05
  • 1
    @KyleK when you enter your age you give a 0 before ?, if a number start with 0, javascript take this value as octal – Hacketo Jan 28 '15 at 14:08
  • You can't have octal if you gave the base to parseInt() (here 10), here it will also return false if you enter "15.4", and it's a checking so we assume user can enter anything, if you trust enough the user to not enter letter, 0 or decimal, then you don't need to check at all. I simply answered the question with no assertion. – KyleK Oct 24 '20 at 15:50
  • `let age = "NaN";` :) – Birbone Jan 08 '21 at 13:55
  • In the next steps you'll probably check the integer result itself such as `if (age >= 0)` because you probably don't want negative integer here, and there 'NaN' won't pass and you'll be safe again. – KyleK Jan 09 '21 at 11:53
4

Prompts always return strings, but since JS is loosely typed language, those Strings will get autocasted into Numbers when needed (Integers are called Numbers in JS), which is why your example works fine.

For a better check, you can use !isNaN.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN

alert(!isNaN('10'));
alert(!isNaN('abc'));
alert(!isNaN(10));

For the lunatic downvoters, here's an optimized version of OP's code:

var age = parseInt(prompt("Please enter your age", "Enter age"), 10);

alert(isNaN(age) ? 'Not a number' : age);
Shomz
  • 37,421
  • 4
  • 57
  • 85
  • 1
    isNaN will no check Interger but Number (float and int): so alert(!isNaN(10.6)); is not correct for an age. – KyleK Jan 28 '15 at 13:59
  • @KyleK We're obviously talking about string vs int... but thanks for the downvote. – Shomz Jan 28 '15 at 14:01
  • The name of the variable is `age`. And the question is check if is an integer. Sorry isNaN is not appropriate. – KyleK Jan 28 '15 at 14:04
  • 2
    @KyleK You will **NEVER**, **EVER** get an integer from a JS prompt. – Shomz Jan 28 '15 at 14:06
  • This is not the problem. I think you did not understand my answear, I exactly said "prompt will always return a string". Please test my code. – KyleK Jan 28 '15 at 14:11
  • 2
    We were never talking about your answer, but bashing mine... I could always tell something equally stupid, like negative numbers are not appropriate for a variable called age, yet your answer doesn't handle them... but that's out of question scope. – Shomz Jan 28 '15 at 14:15
  • I did not bash anything. Why so angry? Every body can give his own answear no? – KyleK Jan 28 '15 at 14:22
0

You can try this one to check if its an integer:

function isInteger(x) {
        return x % 1 === 0;
    }
Kornelito Benito
  • 1,067
  • 1
  • 17
  • 38
  • Could you explain why the original code doesn't work? – JJJ Jan 28 '15 at 13:57
  • Some idiot has more fun giving downvotes instead of writing the answer he thinks is good... sad times. – Shomz Jan 28 '15 at 14:00
  • the code question is really works, give other best solution then – Joe Kdw Jan 28 '15 at 14:01
  • 1
    sad, just demotivates me even contributing. As a new developper, I don't know everything from programming. When I have a working option, I give it. Gave you upvote just for sympathy @Shomz, and off course for the working answer :) – Kornelito Benito Jan 28 '15 at 14:03
  • I gave you upvoted. I feel that whoever contribute here is to HELP even if that's far from the question (for me this is not spam). be patient. keep spirit – Joe Kdw Jan 28 '15 at 14:07
  • 1
    @KornelitoBenito Yeah, sometimes it gets me down as well, and I'm not new here. Keep your chin up, it doesn't happen frequently - it's usually a badly posted/unclear question + a maniac reading it at the moment you post an answer. :) – Shomz Jan 28 '15 at 14:08