Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    Last edited by red566; 03-08-2013 at 05:47 AM.

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,292
    Thanks
    10
    Thanked 583 Times in 564 Posts
    Code:
    if( parseInt(a) == a ){
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    New Coder
    Join Date
    Mar 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    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

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,904
    Thanks
    56
    Thanked 543 Times in 540 Posts
    you mean like
    Code:
    if (!isNaN(a)){
    // do stuff
    }
    ?

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,603
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by xelawho View Post
    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) {...
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    New Coder
    Join Date
    Mar 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    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

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,603
    Thanks
    0
    Thanked 645 Times in 635 Posts
    So use the version xelawho provided since you only need to check if it's a number:

    if (!isNaN(Asking) && 1 === Numberyes){
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    New Coder
    Join Date
    Mar 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    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

  • #9
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,904
    Thanks
    56
    Thanked 543 Times in 540 Posts
    so... what's the question?

  • #10
    New Coder
    Join Date
    Mar 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    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)

  • #11
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,904
    Thanks
    56
    Thanked 543 Times in 540 Posts
    You can probably simplify it to demonstrate the problem. Sounds like you're not breaking out of your while loop when you should

  • #12
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    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.

  • #13
    New Coder
    Join Date
    Mar 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #14
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,603
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by Airblader View Post
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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