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

• 03-08-2013, 12:24 AM
red566
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
• 03-08-2013, 12:26 AM
rnd me
Code:

if( parseInt(a) == a ){
• 03-08-2013, 01:27 AM
red566
Quote:

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
• 03-08-2013, 01:40 AM
xelawho
you mean like
Code:

if (!isNaN(a)){
// do stuff
}

?
• 03-08-2013, 02:10 AM
felgall
Quote:

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) {...
• 03-08-2013, 02:28 AM
red566
Quote:

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
• 03-08-2013, 02:35 AM
felgall
So use the version xelawho provided since you only need to check if it's a number:

if (!isNaN(Asking) && 1 === Numberyes){
• 03-08-2013, 03:05 AM
red566
Quote:

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
• 03-08-2013, 03:37 AM
xelawho
so... what's the question?
• 03-08-2013, 04:02 AM
red566
Quote:

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)
• 03-08-2013, 04:29 AM
xelawho
You can probably simplify it to demonstrate the problem. Sounds like you're not breaking out of your while loop when you should
• 03-08-2013, 05:57 AM
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.
• 03-08-2013, 06:47 AM
red566
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 :D

any way thanks for the help the first part of my game is now unbreakable so now i will start on the game play :D
• 03-08-2013, 06:48 AM
felgall
Quote: