PDA

View Full Version : Resolved how to do If (A == integer){}



red566
Mar 8th, 2013, 12:24 AM
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

rnd me
Mar 8th, 2013, 12:26 AM
if( parseInt(a) == a ){

red566
Mar 8th, 2013, 01:27 AM
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

xelawho
Mar 8th, 2013, 01:40 AM
you mean like


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

?

felgall
Mar 8th, 2013, 02:10 AM
you mean like


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) {...

red566
Mar 8th, 2013, 02:28 AM
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


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

felgall
Mar 8th, 2013, 02:35 AM
So use the version xelawho provided since you only need to check if it's a number:

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

red566
Mar 8th, 2013, 03:05 AM
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

xelawho
Mar 8th, 2013, 03:37 AM
so... what's the question?

red566
Mar 8th, 2013, 04:02 AM
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)

xelawho
Mar 8th, 2013, 04:29 AM
You can probably simplify it to demonstrate the problem. Sounds like you're not breaking out of your while loop when you should

Airblader
Mar 8th, 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.

red566
Mar 8th, 2013, 06:47 AM
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

felgall
Mar 8th, 2013, 06:48 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.

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.