got value of "NaN"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maminx
    New Member
    • Jul 2008
    • 77

    got value of "NaN"

    hello all, i have an error value / "NaN" value that's i;m not expecting of..

    this is i how i check the for the value that's become "NaN"


    [CODE=javascript]new Ajax.Request(ba se_url+'finance _control/cbh/recalculate_ite ms', {postBody: Form.serialize( 'newcbhForm'), onComplete: function(transp ort) {
    data = transport.respo nseText.evalJSO N();
    $('item_amount' ).innerHTML = data.amount;
    max_cashbnknour ut = parseInt(data.m ax_cashbnknouru t);
    if (isNaN(max_cash bnknourut) || isNaN(max_cashb nknourut)){
    alert("Nan error");
    }
    }})[/CODE]

    the max_cashbnknour ut is digit/numeric (0..10).
    When i got "NaN" value, how can i make the value of max_cashbnknour ut become digit/numeric again??tks

    regards,

    maminx
    Last edited by gits; Aug 11 '08, 06:57 AM. Reason: remember to use CODE-Tags !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    could you tell what data.max_cashbn knourut is right after the eval?

    kind regards

    Comment

    • maminx
      New Member
      • Jul 2008
      • 77

      #3
      Originally posted by gits
      could you tell what data.max_cashbn knourut is right after the eval?

      kind regards

      yes that's right, actually i've already use that script logic to another functionality/module, and it's works well...but i have no idea when i;m using that logic/ script for another module, it's get NaN value...

      help please, any idea???

      when the "max_cashbnknou rut" got NaN value, how can i reset the value become integer or the value of "max_cashbnknou rut" after it's casting with parseInt ?

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        just put an alert after the eval-line and alert data.max_cashbn knourut ... it cannot be NaN when it could be parsed to int ... may be data.max_cashbn knourut is undefined? ...

        kind regards

        Comment

        • maminx
          New Member
          • Jul 2008
          • 77

          #5
          Originally posted by gits
          could you tell what data.max_cashbn knourut is right after the eval?

          kind regards

          //max_cashbnknour ut before parsing is 1;
          max_cashbnknour ut = parseInt(data.m ax_cashbnknouru t);
          //max_cashbnknour ut after parsing is NaN;

          Comment

          • maminx
            New Member
            • Jul 2008
            • 77

            #6
            Originally posted by gits
            just put an alert after the eval-line and alert data.max_cashbn knourut ... it cannot be NaN when it could be parsed to int ... may be data.max_cashbn knourut is undefined? ...

            kind regards

            the "max_cashbnknou rut" is undefined when it's not cast with parseInt.

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              the problem is not max_cashbnknour ut that seems to be a global in your code ... but the data.max_cashbn knourut ... i assume it is just missing in your responseText ... you replace the global value 1 with an undefined value ...

              kind regards

              Comment

              • maminx
                New Member
                • Jul 2008
                • 77

                #8
                Originally posted by gits
                the problem is not max_cashbnknour ut that seems to be a global in your code ... but the data.max_cashbn knourut ... i assume it is just missing in your responseText ... you replace the global value 1 with an undefined value ...

                kind regards

                my investigation is i always got NaN value after i make parseInt of "max_cashbnknou rut", before i;m casting the max_cashbnknour ut, the value is 1, and after i make

                max_cashbnknour ut = parseInt(data.m ax_cashbnknouru t)

                the value of max_cashbnknour ut is become "NaN"

                idea please??

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5388

                  #9
                  parseInt(data.max_cashbnknou rut) returns NaN in your case !!! ... so just check your Ajax-Response first! you assign NaN to your variable ... it is not a problem of parseInt ... it MUST be a problem of your Ajax-response ...

                  kind regards

                  Comment

                  • maminx
                    New Member
                    • Jul 2008
                    • 77

                    #10
                    Originally posted by gits
                    parseInt(data.max_cashbnknou rut) returns NaN in your case !!! ... so just check your Ajax-Response first! you assign NaN to your variable ... it is not a problem of parseInt ... it MUST be a problem of your Ajax-response ...

                    kind regards

                    yes of course my problem value of NaN is only in my case, i;m sorry the problem is not in parseInt of course...

                    when i read your message above, i just realize that the problem is in AJAX, and i figure out the solving..it;s in controller that called the AJAX function...

                    anyway, thanks a lot, you had give me an idea, superb !!

                    kind regard, maminx

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5388

                      #11
                      glad to hear you found the error ... post back to the forum in case you have more questions ...

                      kind regards

                      Comment

                      • LauraNutt
                        New Member
                        • Oct 2020
                        • 7

                        #12
                        Before selecting a method for checking for NaN, how should you not check for NaN?

                        NaN is a bizarre value in JavaScript, as it does not equal itself when compared, either with the loose equality (==) or strict equality (===) operator. NaN is the only value in the entire language which behaves in this manner with regards to comparisons.

                        For example, if parseInt(“a”) returns NaN, then parseInt(“a”) === NaN will return false. This may seem strange, but it makes perfect sense after thinking about what NaN really is.

                        NaN doesn’t tell you what something is, it tells you what it isn’t.

                        These two different strings being passed to parseInt() will both return NaN.

                        parseInt(“abc”) // NaN
                        parseInt(“def”) // NaN
                        Both statements return NaN, but are they really the same? Maybe, but it certainly makes sense why JavaScript would disagree, given that they are derived from different string arguments.

                        Here are a few examples of strict inequality comparisons, which demonstrate the inconsistency of NaN.

                        2 !== 2 // false
                        true !== true // false
                        “abc” !== “abc” // false
                        ...
                        NaN !== NaN // true
                        Method 1: isNaN or Number.isNaN

                        JavaScript has a built-in method, appropriately named “isNaN,” which checks for NaN. There is a newer function called Number.isNaN, which is included in the ES2015 spec.

                        The difference between isNaN and Number.isNaN is that isNaN coerces the argument into a number type. To avoid complicated and unexpected outcomes, it is often advised to use the newer, more robust Number.isNaN to avoid these side effects. Number.isNaN does not perform any forcible type conversion, so it simply returns the boolean based on the parameter.

                        Here is an example of the difference between the two methods:

                        isNaN(undefined ) // true
                        Number.isNaN(un defined) // false
                        isNaN, when passed undefined, returns true because undefined becomes NaN after number coercion. You can test this yourself by running Number(undefine d). You will find that it returns NaN.

                        Number.isNaN, on the other hand, returns false. This is because no coercion takes place, and undefined is not NaN, it is simply undefined.

                        It is also important to note that Number.isNaN is a newer (ES2015) method in JavaScript, so browser support for Number.isNaN is not as stable as isNaN, which has been around since ES1 (1997).

                        Method 2: Object.is

                        Object.is is a JavaScript method which checks for sameness. It generally performs the same evaluations as a strict equality operator (===), although it treats NaN differently from strict equality.

                        Object.is(0, -0) will return false, while 0 === -0 will return true. Comparisons of 0 and -0 differ, as do comparisons of NaN. This concept is called “Same-value-zero equality.”

                        NaN === NaN // false
                        Object.is(NaN, NaN) // true
                        Object.is(NaN, NaN) will in fact return true, while we already know that NaN === NaN returns false. That makes this yet another valid way to check if something is not a number.

                        Comment

                        Working...