473,765 Members | 2,203 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jul 23 '05 #1
5 1877
Lee
Steve said:
function checkDate(Analy sis,MyTime){
alert("checkDat e function called");
if(Analysis.RSD I || Analysis.pH){ The problem is that it keeps entering the first if statement which
should only evaluate as true if pH or Residual Disinfectants is
checked.


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

Jul 23 '05 #2
Steve wrote:
[...]
<SNIP>
<form method="post" action="" name="Analysis" >
<table>
<tr>
Please don't use tabs for posted code, use double spaces.
<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
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...>
Times" >
</td>
</tr>
</table>
</form>
</SNIP>

And here is the .js

<SNIP>
function Calc(Analysis){
MyTime = new Date();
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.
checkDate(Analy sis,MyTime);
You call checkDate(), but don't do anything with the result:

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

return;
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.
}

function checkDate(Analy sis,MyTime){
alert("checkDat e function called");
if(Analysis.RSD I || Analysis.pH){
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){

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);
And if no checkboxes are checked? I've added in an extra return to
return MyTime - which should be unmodified (i.e. now).

[...] 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.


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
Jul 23 '05 #3
Thank You!
That turned out to be exactly the answer I needed

Jul 23 '05 #4
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.

Jul 23 '05 #5
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.

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4044
by: Eric Linders | last post by:
Hello, We have a form that is collecting user input. There are three fields for each phone number (area code, prefix and suffix). I'm trying to use a simple if statement to confirm if the first character in any of the fields is "0". If it is, then I just redirect them to a different page because it's someone that is just messing around. Unfortunately, whenever the form loads, the if statement automatically evaluates to true and the user...
5
1735
by: Andreas Paasch | last post by:
I'm attempting to trigger things based on time. I have one shop that has opening hours, closing hours and lunch hours store as full hour values, integer, in MySQL. I retrieve them, based on comparisons either shop is open, about to close or actually closed. When open, nothing particular is written, when closed, customer is informed orders can't be executed, shop is closed. But the last one, right about 15 minutes before closing time,...
8
2194
by: NotGiven | last post by:
I have the code below that always evaluates to true. Why and what do I do about it? Many thanks in advance! In the code, I have tried sending in the URL ?indentNum=23 ?indentNum=23.2 ?indentNum=23sadkjsi8 ?indentNum=aanns No matter what I throw at it, it always evaluates to true despite echo'ing
4
1717
by: shumaker | last post by:
I'm wondering how/why this query works. Trying to get my head wrapped around SQL. Basically the Query deletes from the Import table all records that are already in FooStrings so that when I do an insert from the FooStringsImport table into the FooStrings table, then I won't get primary key violations. DELETE FROM FooStringsImport WHERE EXISTS (SELECT * FROM FooStrings WHERE FooStringsImport.FooKey = FooStrings.FooKey)
19
1452
by: Jordan | last post by:
Let me just start by saying I'm a very accomplished ASP programmer. I need to rely on that becuase this error boggles the mind. Just today, I had to troubleshoot an error in one of my applications and I tracked it down to a single condition statement. Here it is (very complex code to follow): <code> If (CSng(ccTuition) <> tuition) Then
3
1492
by: Ben | last post by:
Hi There I am doing some unit testing at the moment, and the majority of the leg work involves taking two objects (expected vs actual) and verifying that their properties are equal. The objects that I am testing and their properties are diverse, including enums, basic system types, arrays and nested types. The objects themselves were not written by me, and do not have any overrides for equals, compare ... etc.
6
1553
by: sparks | last post by:
extracalc = Switch(Me.Parent.Race_Black = -1 And Me.Parent.Sex = "Female", 1.952, Me.Parent.Race_Black = -1, 1.21, Me.Parent.Sex = "Female", 0.742, 1) I look at this and say ok if race = black and sex = female then extracalc = 1.952 if race = black then extracalc = 1.21 if sex = female then extracalc = .742 else
7
1544
by: odysseus654 | last post by:
I have just discovered the "with" statement, which up until now I have only known as "that which should never be used". I would like to evaluate some commands (such as function definitions and the like) within the global context, would this accomplish the task? Are there better ways to deal with this? (Note that I am making dynamic execution a requirement) function executeAsGlobal(cmd) { with(window) exec(cmd);
2
1253
by: waltbrad | last post by:
The script comes from Mark Lutz's Programming Python. It is the second line of a script that will launch a python program on any platform. import os, sys pyfile = (sys.platform == 'win' and 'python.exe') or 'python' Okay, run on a win32 machine, pyfile evaluates to python.exe That makes sense. Because the first condition is true and 'python.exe'
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.