Why would this IF statement evaluate as TRUE?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    Why would this IF statement evaluate as TRUE?

    Hello, I've been a PHP programmer for a number of years and have just
    started to learn JS. My Employer (a water analysis lab) wants what
    should be a very simple .js written that basically takes sample hold
    time data from EPA regulations and spits out when a sample would
    expire, so we can properly label the thing.

    The problem is that the .js I have written appears to be doing
    something unexpected.

    The Analysis options are presented as check boxes to the user, and
    whichever analysis has the shortest holding time is the one we base
    our maximum holding time on.

    Here is a snip of the HTML...

    <SNIP>
    <form method="post" action="" name="Analysis" >
    <table>
    <tr>
    <td colspan="2">Sam ple ID:</td><td><input maxlength=30
    name="ID"></td>
    </tr>
    <tr>
    <td>Analyis: (Check All That Apply)</td>
    </tr>
    <tr>
    <td>
    <input type="checkbox" name="METALS" >Metals (except Hg)<br>
    <input type="checkbox" name="Hg" >Mercury (Hg)<br>
    <input type="checkbox" name="ALK" >Alkalinity<b r>
    <input type="checkbox" name="H3N" >Ammonia<br>
    <input type="Submit" onClick=Calc(An alysis) value="Calculat e Hold
    Times" >
    </td>
    </tr>
    </table>


    </form>
    </SNIP>

    And here is the .js

    <SNIP>
    function Calc(Analysis){
    MyTime = new Date();
    checkDate(Analy sis,MyTime);
    //alert("checkDat e cleared success!"+ MyTime);
    alert("The expiration of the holding time for sample will occur
    "+MyTime);

    return;
    }

    function checkDate(Analy sis,MyTime){
    alert("checkDat e function called");
    if(Analysis.RSD I || Analysis.pH){
    alert("Entered if statement 1"+ MyTime);
    MyTime.setHours (MyTime.getHour s()+1);
    return MyTime;
    }

    if(Analysis.ODO R){
    alert("Entered if statement 2");
    MyTime.setDate( MyTime.getDate( )+1);
    return;
    }
    </SNIP>

    The problem is that it keeps entering the first if statement which
    should only evaluate as true if pH or Residual Disinfectants is
    checked. At which time it adds 1 to the hour field and then exits.

    This seems VERY counter-intuitive.

    I have manually added alerts to check for this behavior, but as you
    can see, if you run the sample it just enters the first IF as though
    it were true and then completely skips anything after the return
    MyTime function.

    Any Ideas on what may be wrong here?

    Thanks in Advance!

    p.s. I'm not sure if this makes a difference for our purposes or not,
    it doesn't appear to, but anyways the .js is all contained between
    HEAD tags.
  • Lee

    #2
    Re: Why would this IF statement evaluate as TRUE?

    Steve said:
    [color=blue]
    > function checkDate(Analy sis,MyTime){
    > alert("checkDat e function called");
    > if(Analysis.RSD I || Analysis.pH){[/color]
    [color=blue]
    >The problem is that it keeps entering the first if statement which
    >should only evaluate as true if pH or Residual Disinfectants is
    >checked.[/color]

    If there is a form field named Analysis.RSDI, then the logical
    expression Analysis.RSDI will always evaluate to true.

    You seem want to test the "checked" attribute of Analysis.RSDI:

    if(Analysis.RSD I.checked || Analysis.pH.che cked) {

    Comment

    • RobG

      #3
      Re: Why would this IF statement evaluate as TRUE?

      Steve wrote:
      [...][color=blue]
      > <SNIP>
      > <form method="post" action="" name="Analysis" >
      > <table>
      > <tr>[/color]

      Please don't use tabs for posted code, use double spaces.
      [color=blue]
      > <td colspan="2">Sam ple ID:</td><td><input maxlength=30
      > name="ID"></td>
      > </tr>
      > <tr>
      > <td>Analyis: (Check All That Apply)</td>
      > </tr>
      > <tr>
      > <td>
      > <input type="checkbox" name="METALS" >Metals (except Hg)<br>
      > <input type="checkbox" name="Hg" >Mercury (Hg)<br>
      > <input type="checkbox" name="ALK" >Alkalinity<b r>
      > <input type="checkbox" name="H3N" >Ammonia<br>
      > <input type="Submit" onClick=Calc(An alysis) value="Calculat e Hold[/color]

      Here you are passing a reference to the form "Analysis", but better to
      use this.form (and all attributes should be in quotes, including
      JavaScript):

      <input type="Submit" onClick="Calc(t his.form);" value...>
      [color=blue]
      > Times" >
      > </td>
      > </tr>
      > </table>
      >
      >
      > </form>
      > </SNIP>
      >
      > And here is the .js
      >
      > <SNIP>
      > function Calc(Analysis){
      > MyTime = new Date();[/color]

      Don't be confident that the date object you create will be correct.
      You may want to ensure it by validating against a datum sent from the
      server.
      [color=blue]
      > checkDate(Analy sis,MyTime);[/color]

      You call checkDate(), but don't do anything with the result:

      var x = checkDate(Analy sis,MyTime);
      [color=blue]
      > //alert("checkDat e cleared success!"+ MyTime);
      > alert("The expiration of the holding time for sample will occur
      > "+MyTime);[/color]

      alert("The ... occur at " + x);
      [color=blue]
      >
      > return;[/color]

      It's usual to set the return value if it's important, I think you need
      to do some validation and check stuff and if that fails, return false
      so the form doesn't submit. If it all works, return true so it does.

      I've deleted it in the code below just for now.
      [color=blue]
      > }
      >
      > function checkDate(Analy sis,MyTime){
      > alert("checkDat e function called");
      > if(Analysis.RSD I || Analysis.pH){[/color]

      This if statement just checks if the form Analysis has an element
      called RSDI or pH (incidentally, these don't match the element names in
      the form, I've fixed that below).

      It will always evaluate to true as long as you have elements called
      RSDI or pH (which you don't, see above). You want to see of they've
      been checked:

      if(Analysis.RSD I.checked || Analysis.pH.che cked){

      [color=blue]
      > alert("Entered if statement 1"+ MyTime);
      > MyTime.setHours (MyTime.getHour s()+1);
      > return MyTime;
      > }
      >
      > if(Analysis.ODO R){
      > alert("Entered if statement 2");
      > MyTime.setDate( MyTime.getDate( )+1);[/color]

      And if no checkboxes are checked? I've added in an extra return to
      return MyTime - which should be unmodified (i.e. now).

      [...][color=blue]
      > p.s. I'm not sure if this makes a difference for our purposes or not,
      > it doesn't appear to, but anyways the .js is all contained between
      > HEAD tags.[/color]

      That is probably the best place to put it. Once you have it working,
      put it into a separate .js file for easier maintenance.

      The code below barely gets you going, there is a lot of work to be done
      yet. You must check to see how many checkboxes have been checked and
      only select the lowest value, and you should send a date/time stamp
      from the server that is used on the client to ensure the correct time
      even if the local machine's clock is incorrect.

      If no checkboxes are checked, the code returns MyTime.

      The logic is that the shorter times are checked first, however it would
      be better to actually check for the shortest time so the logic is based
      on fact rather than the order in which things are processed by the
      code.

      The checkbox names didn't even match your sample code, so please check
      everything thoroughly and post back here with any problems.

      Cheers.

      <html><head><ti tle>fred</title>

      <script type="text/javascript">
      function Calc(f){
      MyTime = new Date();
      var x = checkDate(f,MyT ime);
      alert("The expiration of the holding time" +
      " for sample will occur at " + x);
      return true;

      }

      function checkDate(f,MyT ime) {
      if(f.elements['Hg'].checked || f.elements['ALK'].checked){
      MyTime.setHours (MyTime.getHour s()+1);
      return MyTime;
      }
      if(f.elements['H3N'].checked){
      MyTime.setHours (MyTime.getHour s()+4);
      return MyTime;
      }
      return MyTime;
      }

      </script>

      </head>
      <body>
      <form action="" name="Analysis" >
      <table><tr>
      <td colspan="2">Sam ple ID:</td>
      <td><input maxlength=30 name="ID"></td>
      </tr><tr>
      <td>Analyis: (Check All That Apply)</td>
      </tr><tr>
      <td>
      <input type="checkbox" name="METALS" >Metals (except Hg)<br>
      <input type="checkbox" name="Hg" >Mercury (Hg)<br>
      <input type="checkbox" name="ALK" >Alkalinity<b r>
      <input type="checkbox" name="H3N" >Ammonia<br>
      <input type="Submit" onClick="return Calc(this.form) ;"
      value="Calculat e Hold Times">
      </td>
      </tr>
      </table>
      </form>
      </body></html>

      --
      Rob

      Comment

      • gr82meetu78@yahoo.com

        #4
        Re: Why would this IF statement evaluate as TRUE?

        Thank You!
        That turned out to be exactly the answer I needed

        Comment

        • gr82meetu78@yahoo.com

          #5
          Re: Why would this IF statement evaluate as TRUE?

          Sorry about that I used snippets for berevity, but for the record the
          code did have all the form fields correct, I just missed the .checked
          method. As is addressed by another kind person in this NG.

          As for having the logic evaluating the Analysis by holding time, with
          just the order of operations, I'll admit I did take a shortcut here, it
          should in fact compare them based on holding time, however I still
          don't see a way to do this in .js, care to show me an example of how
          this comparison would be done under best practices. Again thanks in
          advance for your replies.

          Comment

          • gr82meetu78@yahoo.com

            #6
            Re: Why would this IF statement evaluate as TRUE?

            Sorry about that I used snippets for berevity, but for the record the
            code did have all the form fields correct, I just missed the .checked
            method. As is addressed by another kind person in this NG.

            As for having the logic evaluating the Analysis by holding time, with
            just the order of operations, I'll admit I did take a shortcut here, it
            should in fact compare them based on holding time, however I still
            don't see a way to do this in .js, care to show me an example of how
            this comparison would be done under best practices. Again thanks in
            advance for your replies.

            Comment

            Working...