Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: how to do If (A == integer){}

1. ## how to do If (A == integer){}

how do i go about checking if a var is a integer or not so i can do something like
if (A == integer){
B = 42
}

sorry i dont know what to call this and i will fix it up later when i know how to do it

• Code:
`if( parseInt(a) == a ){`

• Originally Posted by rnd me
Code:
`if( parseInt(a) == a ){`
sorry it is not working
i need it to do the IF if the var is a Int eg it is just numbers

• you mean like
Code:
```if (!isNaN(a)){
// do stuff
}```
?

• Originally Posted by xelawho
you mean like
Code:
```if (!isNaN(a)){
// do stuff
}```
?
That checks if it is a number but not if it is an integer. To specifically test if it is an integer you'd need to use Math.floor() and check if that value is the same as the original - ie remove any decimal places and then compare.

`if (Math.floor(a) == a) {...`

• Originally Posted by felgall
That checks if it is a number but not if it is an integer. To specifically test if it is an integer you'd need to use Math.floor() and check if that value is the same as the original - ie remove any decimal places and then compare.

`if (Math.floor(a) == a) {...`
ok here is the code i am useing
Code:
```	if (//Asking is a number && Numberyes == 1){
document.wrte (Asking + "This is a number" + Telling)
Telling = 0
}```
the document.write is just to see if it is working
Asking, Numberyes and Telling are var's

so if Asking is a number and Numberyes == 1
then Telling = 1

do you get what i mean

• So use the version xelawho provided since you only need to check if it's a number:

`if (!isNaN(Asking) && 1 === Numberyes){`

• Originally Posted by felgall
So use the version xelawho provided since you only need to check if it's a number:

`if (!isNaN(Asking) && 1 === Numberyes){`
ok so i have all of this in a function with other stuff and what it dos is checks what the user input is

then the users in put is in a loop so while Telling = 0 it will do the loop

the point of that is so if the user puts something in that has no "function" it will just ask it again until you put something in that makes Telling != 0 and the script contuses

witch works fine when it is a simple yes or no question and i can put in all the ways of doing yes or no but when it is something like a number... well i cant put all the combos in :P

• so... what's the question?

• Originally Posted by xelawho
so... what's the question?
o sorry i was in the middle of class :P

that did not work and i dont know why but it broke my script when i put a number as the user input

it just stopped running the script past that point

if u want i can post the hole script but it is 416 lines (a lot of spacing tho)

• You can probably simplify it to demonstrate the problem. Sounds like you're not breaking out of your while loop when you should

• isNaN(x) or parseInt(x) == x doesn't check if x is a number/integer, it just checks if x has a value representing a number/integer (it may very well actually be a string). This could or could not be the intended behavior, so I'm just pointing it out.

• ok i found a nice "simple" way of doing it then found the problem with the other ways
i had a spelling error just after that function :P sorry

the "simple" other way i tried was to simply do something like Asking - Asking and if it was a number it would == 0 so there for it is a number

any way thanks for the help the first part of my game is now unbreakable so now i will start on the game play

isNaN(x) or parseInt(x) == x doesn't check if x is a number/integer, it just checks if x has a value representing a number/integer (it may very well actually be a string). This could or could not be the intended behavior, so I'm just pointing it out.
In that case you could use:

`if (x === +x)`

which will only return true if x actually is a number and not just something that can be converted to a number.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•