2

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.

7
  • It displays data is not an integer no matter what. Even if i put an integer. Commented Jan 28, 2015 at 13:55
  • @user3072143 Check the code snippet :) Commented Jan 28, 2015 at 13:56
  • The code you've provided works just fine (click on the "run code snippet" button). The problem is somewhere else. Commented Jan 28, 2015 at 13:56
  • Works fine for me as well, made a fiddle: jsfiddle.net/legolandbridge/cx1a1g23 Commented Jan 28, 2015 at 13:56
  • this has ben answered before (use ===): stackoverflow.com/questions/14636536/… Commented Jan 28, 2015 at 13:57

3 Answers 3

14

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.

Sign up to request clarification or add additional context in comments.

9 Comments

== is not checking the type of the variable, it should have the same behavior
Yes I know, so I use === that check the type.
Could you explain why the original code doesn't work?
@user3072143 "15" == 15 and "15" === "" + 15 result with the same value , how could it solve your problem ?
@KyleK when you enter your age you give a 0 before ?, if a number start with 0, javascript take this value as octal
|
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);

7 Comments

isNaN will no check Interger but Number (float and int): so alert(!isNaN(10.6)); is not correct for an age.
@KyleK We're obviously talking about string vs int... but thanks for the downvote.
The name of the variable is age. And the question is check if is an integer. Sorry isNaN is not appropriate.
@KyleK You will NEVER, EVER get an integer from a JS prompt.
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.
|
0

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

function isInteger(x) {
        return x % 1 === 0;
    }

6 Comments

Could you explain why the original code doesn't work?
Some idiot has more fun giving downvotes instead of writing the answer he thinks is good... sad times.
the code question is really works, give other best solution then
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 :)
@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. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.