if it's NaN make it 0

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?cm9kY2hhcg==?=

    if it's NaN make it 0

    hey all,
    ph_area_1_appra iserscore =
    isNaN(ph_area_1 _appraiserscore )?0:ph_area_1_a ppraiserscore;

    is this the only way to say this?

    thanks,
    rodchar
  • =?Utf-8?B?cm9kY2hhcg==?=

    #2
    RE: if it's NaN make it 0

    this is a javascript question by the way.

    "rodchar" wrote:
    hey all,
    ph_area_1_appra iserscore =
    isNaN(ph_area_1 _appraiserscore )?0:ph_area_1_a ppraiserscore;
    >
    is this the only way to say this?
    >
    thanks,
    rodchar

    Comment

    • Patrice

      #3
      Re: if it's NaN make it 0

      Seems good. What is the problem ?

      Another option would to avoid NaN in first place...

      --
      Patrice

      "rodchar" <rodchar@discus sions.microsoft .coma écrit dans le message de
      groupe de discussion : 13B16175-CA6E-4810-8EAE-D428E6309672@mi crosoft.com...
      this is a javascript question by the way.
      >
      "rodchar" wrote:
      >
      >hey all,
      >ph_area_1_appr aiserscore =
      >isNaN(ph_area_ 1_appraiserscor e)?0:ph_area_1_ appraiserscore;
      >>
      >is this the only way to say this?
      >>
      >thanks,
      >rodchar

      Comment

      • Anthony Jones

        #4
        Re: if it's NaN make it 0


        "rodchar" <rodchar@discus sions.microsoft .comwrote in message
        news:B66CE703-00E1-47A6-8C06-A075FD781488@mi crosoft.com...
        hey all,
        ph_area_1_appra iserscore =
        isNaN(ph_area_1 _appraiserscore )?0:ph_area_1_a ppraiserscore;
        >
        is this the only way to say this?
        >
        Yes but you could always place in a function to keep your code tidy. I
        would add a method to the Number prototype:-


        Number.prototyp e.valueOrZero = function()
        { return isNaN(this) ? 0 : this.valueOf(); }


        Now you could use:-

        ph_area_1_appra iserscore = ph_area_1_appra iserscore.value OrZero();

        Which would look better (only slightly because the variable name is so
        long).

        Of course this could be done at source since the most likely source of a NaN
        is a parse. E.g.

        ph_area_1_appra iserscore = parseInt(sSomeS ource).valueOrZ ero();


        BTW do you have other variables that begin with the prefix ph_area_1_ ?
        Also do you have an even greater range of variables that begin with
        ph_area_n_ where n is from a set of integers?

        If so isn't that telling something? If not why such a mouthfull of a
        variable name?




        --
        Anthony Jones - MVP ASP/ASP.NET



        Comment

        • =?Utf-8?B?cm9kY2hhcg==?=

          #5
          Re: if it's NaN make it 0

          BTW do you have other variables that begin with the prefix ph_area_1_ ?
          Also do you have an even greater range of variables that begin with
          ph_area_n_ where n is from a set of integers?
          >
          If so isn't that telling something? If not why such a mouthfull of a
          variable name?
          ph_area_1_appra iser_score
          ph_area_2_appra iser_score
          ph_area_3_appra iser_score
          ph_area_4_appra iser_score
          ph_area_5_appra iser_score

          what do you suggest?


          "Anthony Jones" wrote:
          >
          "rodchar" <rodchar@discus sions.microsoft .comwrote in message
          news:B66CE703-00E1-47A6-8C06-A075FD781488@mi crosoft.com...
          hey all,
          ph_area_1_appra iserscore =
          isNaN(ph_area_1 _appraiserscore )?0:ph_area_1_a ppraiserscore;

          is this the only way to say this?
          >
          Yes but you could always place in a function to keep your code tidy. I
          would add a method to the Number prototype:-
          >
          >
          Number.prototyp e.valueOrZero = function()
          { return isNaN(this) ? 0 : this.valueOf(); }
          >
          >
          Now you could use:-
          >
          ph_area_1_appra iserscore = ph_area_1_appra iserscore.value OrZero();
          >
          Which would look better (only slightly because the variable name is so
          long).
          >
          Of course this could be done at source since the most likely source of a NaN
          is a parse. E.g.
          >
          ph_area_1_appra iserscore = parseInt(sSomeS ource).valueOrZ ero();
          >
          >
          BTW do you have other variables that begin with the prefix ph_area_1_ ?
          Also do you have an even greater range of variables that begin with
          ph_area_n_ where n is from a set of integers?
          >
          If so isn't that telling something? If not why such a mouthfull of a
          variable name?
          >
          >
          >
          >
          --
          Anthony Jones - MVP ASP/ASP.NET
          >
          >
          >
          >

          Comment

          • =?Utf-8?B?cm9kY2hhcg==?=

            #6
            Re: if it's NaN make it 0

            i have 5 appraiser factors rated 1-5.
            when the appraiser is on a new review date all scores aren't calculated yet.
            so when the first rating is entered the other factor labels are blank.
            and what i want to happen is a running total at the bottom.
            since the other factors are blank and not a number how do i
            add numbers with all the others that are NaN?

            "Patrice" wrote:
            Seems good. What is the problem ?
            >
            Another option would to avoid NaN in first place...
            >
            --
            Patrice
            >
            "rodchar" <rodchar@discus sions.microsoft .coma crit dans le message de
            groupe de discussion : 13B16175-CA6E-4810-8EAE-D428E6309672@mi crosoft.com...
            this is a javascript question by the way.

            "rodchar" wrote:
            hey all,
            ph_area_1_appra iserscore =
            isNaN(ph_area_1 _appraiserscore )?0:ph_area_1_a ppraiserscore;
            >
            is this the only way to say this?
            >
            thanks,
            rodchar
            >
            >

            Comment

            • Anthony Jones

              #7
              Re: if it's NaN make it 0

              "rodchar" <rodchar@discus sions.microsoft .comwrote in message
              news:ED37980C-ADA6-49D7-806E-255222D48BE5@mi crosoft.com...
              BTW do you have other variables that begin with the prefix ph_area_1_ ?
              Also do you have an even greater range of variables that begin with
              ph_area_n_ where n is from a set of integers?

              If so isn't that telling something? If not why such a mouthfull of a
              variable name?
              ph_area_1_appra iser_score
              ph_area_2_appra iser_score
              ph_area_3_appra iser_score
              ph_area_4_appra iser_score
              ph_area_5_appra iser_score
              >
              what do you suggest?
              >

              At least an array of scores. However the fact that the variable has a clear
              indexer in the middle variable name implies that there may be other
              variables such as:-

              ph_area_2_some_ other_property

              Which would might describe some other property of a 'ph_area object'.

              So perhaps an array objects is actually called for e.g.:-

              function PhArea() {}
              PhArea.prototyp e.putAppraiserS core = function(value)
              {
              this.appraiserS core = parseInt(value) .valueOrZero();
              }

              var aPhArea = new Array(5);
              aPhArea.push(ne w PhArea())
              aPhArea.push(ne w PhArea())
              aPhArea.push(ne w PhArea())
              aPhArea.push(ne w PhArea())
              aPhArea.push(ne w PhArea())

              aPhArea(0).putA ppraiserScore(' 5')
              aPhArea(1).putA ppraiserScore(' 0')
              aPhArea(2).putA ppraiserScore(' rubbish')
              aPhArea(3).putA ppraiserScore(' 10 green bottles') //what should happen here?
              aPhArea(4).putA ppraiserScore(' 5,000,000') // and here?

              for (var i = 0; i < aPhArea.length; i++)
              {
              var phArea = aPhArea(i);

              // code that does stuff with each phArea object.

              }


              When you find yourself creating many variables you should ask yourself, Is
              there actually a collection of some form here? and, Do a set of variables
              actually describe an object?


              --
              Anthony Jones - MVP ASP/ASP.NET


              Comment

              • Patrice

                #8
                Re: if it's NaN make it 0

                Could they be 0 rather than blank to say they are missing ? You could also
                just test if they are blank to ignore them in your calculation.

                Basically my personal preference is to avoid producing a somewhat
                inconsistent result rather than to let this result to happen and recover
                from there (a bit as you sometimes see people that let an error to occur and
                recover from there when by personal preference is to avoid the error to
                happen in the first place).

                --
                Patrice

                "rodchar" <rodchar@discus sions.microsoft .coma écrit dans le message de
                groupe de discussion : F385E636-DF02-415D-8A2B-ABA305CF5FDC@mi crosoft.com...
                i have 5 appraiser factors rated 1-5.
                when the appraiser is on a new review date all scores aren't calculated
                yet.
                so when the first rating is entered the other factor labels are blank.
                and what i want to happen is a running total at the bottom.
                since the other factors are blank and not a number how do i
                add numbers with all the others that are NaN?
                >
                "Patrice" wrote:
                >
                >Seems good. What is the problem ?
                >>
                >Another option would to avoid NaN in first place...
                >>
                >--
                >Patrice
                >>
                >"rodchar" <rodchar@discus sions.microsoft .coma crit dans le message de
                >groupe de discussion :
                >13B16175-CA6E-4810-8EAE-D428E6309672@mi crosoft.com...
                this is a javascript question by the way.
                >
                "rodchar" wrote:
                >
                >hey all,
                >ph_area_1_appr aiserscore =
                >isNaN(ph_area_ 1_appraiserscor e)?0:ph_area_1_ appraiserscore;
                >>
                >is this the only way to say this?
                >>
                >thanks,
                >rodchar
                >>
                >>

                Comment

                • =?Utf-8?B?cm9kY2hhcg==?=

                  #9
                  Re: if it's NaN make it 0

                  At least an array of scores. However the fact that the variable has a clear
                  indexer in the middle variable name implies that there may be other
                  variables such as:-
                  >
                  ph_area_2_some_ other_property
                  >
                  That is correct, there is
                  ph_area_1_emplo yee_score
                  ph_area_2_emplo yee_score
                  ph_area_3_emplo yee_score
                  ph_area_4_emplo yee_score
                  ph_area_5_emplo yee_score

                  "Anthony Jones" wrote:
                  "rodchar" <rodchar@discus sions.microsoft .comwrote in message
                  news:ED37980C-ADA6-49D7-806E-255222D48BE5@mi crosoft.com...
                  BTW do you have other variables that begin with the prefix ph_area_1_ ?
                  Also do you have an even greater range of variables that begin with
                  ph_area_n_ where n is from a set of integers?
                  >
                  If so isn't that telling something? If not why such a mouthfull of a
                  variable name?
                  ph_area_1_appra iser_score
                  ph_area_2_appra iser_score
                  ph_area_3_appra iser_score
                  ph_area_4_appra iser_score
                  ph_area_5_appra iser_score

                  what do you suggest?
                  >
                  >
                  At least an array of scores. However the fact that the variable has a clear
                  indexer in the middle variable name implies that there may be other
                  variables such as:-
                  >
                  ph_area_2_some_ other_property
                  >
                  Which would might describe some other property of a 'ph_area object'.
                  >
                  So perhaps an array objects is actually called for e.g.:-
                  >
                  function PhArea() {}
                  PhArea.prototyp e.putAppraiserS core = function(value)
                  {
                  this.appraiserS core = parseInt(value) .valueOrZero();
                  }
                  >
                  var aPhArea = new Array(5);
                  aPhArea.push(ne w PhArea())
                  aPhArea.push(ne w PhArea())
                  aPhArea.push(ne w PhArea())
                  aPhArea.push(ne w PhArea())
                  aPhArea.push(ne w PhArea())
                  >
                  aPhArea(0).putA ppraiserScore(' 5')
                  aPhArea(1).putA ppraiserScore(' 0')
                  aPhArea(2).putA ppraiserScore(' rubbish')
                  aPhArea(3).putA ppraiserScore(' 10 green bottles') //what should happen here?
                  aPhArea(4).putA ppraiserScore(' 5,000,000') // and here?
                  >
                  for (var i = 0; i < aPhArea.length; i++)
                  {
                  var phArea = aPhArea(i);
                  >
                  // code that does stuff with each phArea object.
                  >
                  }
                  >
                  >
                  When you find yourself creating many variables you should ask yourself, Is
                  there actually a collection of some form here? and, Do a set of variables
                  actually describe an object?
                  >
                  >
                  --
                  Anthony Jones - MVP ASP/ASP.NET
                  >
                  >
                  >

                  Comment

                  • =?Utf-8?B?cm9kY2hhcg==?=

                    #10
                    Re: if it's NaN make it 0

                    aPhArea.push(ne w PhArea())
                    aPhArea.push(ne w PhArea())
                    What does .push do?


                    "Anthony Jones" wrote:
                    "rodchar" <rodchar@discus sions.microsoft .comwrote in message
                    news:ED37980C-ADA6-49D7-806E-255222D48BE5@mi crosoft.com...
                    BTW do you have other variables that begin with the prefix ph_area_1_ ?
                    Also do you have an even greater range of variables that begin with
                    ph_area_n_ where n is from a set of integers?
                    >
                    If so isn't that telling something? If not why such a mouthfull of a
                    variable name?
                    ph_area_1_appra iser_score
                    ph_area_2_appra iser_score
                    ph_area_3_appra iser_score
                    ph_area_4_appra iser_score
                    ph_area_5_appra iser_score

                    what do you suggest?
                    >
                    >
                    At least an array of scores. However the fact that the variable has a clear
                    indexer in the middle variable name implies that there may be other
                    variables such as:-
                    >
                    ph_area_2_some_ other_property
                    >
                    Which would might describe some other property of a 'ph_area object'.
                    >
                    So perhaps an array objects is actually called for e.g.:-
                    >
                    function PhArea() {}
                    PhArea.prototyp e.putAppraiserS core = function(value)
                    {
                    this.appraiserS core = parseInt(value) .valueOrZero();
                    }
                    >
                    var aPhArea = new Array(5);
                    aPhArea.push(ne w PhArea())
                    aPhArea.push(ne w PhArea())
                    aPhArea.push(ne w PhArea())
                    aPhArea.push(ne w PhArea())
                    aPhArea.push(ne w PhArea())
                    >
                    aPhArea(0).putA ppraiserScore(' 5')
                    aPhArea(1).putA ppraiserScore(' 0')
                    aPhArea(2).putA ppraiserScore(' rubbish')
                    aPhArea(3).putA ppraiserScore(' 10 green bottles') //what should happen here?
                    aPhArea(4).putA ppraiserScore(' 5,000,000') // and here?
                    >
                    for (var i = 0; i < aPhArea.length; i++)
                    {
                    var phArea = aPhArea(i);
                    >
                    // code that does stuff with each phArea object.
                    >
                    }
                    >
                    >
                    When you find yourself creating many variables you should ask yourself, Is
                    there actually a collection of some form here? and, Do a set of variables
                    actually describe an object?
                    >
                    >
                    --
                    Anthony Jones - MVP ASP/ASP.NET
                    >
                    >
                    >

                    Comment

                    • Anthony Jones

                      #11
                      Re: if it's NaN make it 0

                      "rodchar" <rodchar@discus sions.microsoft .comwrote in message
                      news:16E2C68B-5EC7-49A9-A762-91CEB38A1110@mi crosoft.com...
                      aPhArea.push(ne w PhArea())
                      aPhArea.push(ne w PhArea())
                      >
                      What does .push do?
                      >
                      It pushes a new entry into the array. It is equivalent to :-

                      aPhArea[aPhArea.length] = new PhArea()





                      --
                      Anthony Jones - MVP ASP/ASP.NET


                      Comment

                      Working...