473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Greater than operator problem

Hi All,

Can anyone tell me why the following code does not evaluate correctly
on my web page:

function calcLabels()
{

alert('Stage 1');
if (document.labels.elements.frmBundlesPerBox.value 0)
{
alert('Stage 2');
alert('frmQuantityDelivered = ' +
document.labels.elements.frmQuantityDelivered.valu e + ' -
frmMaxQuantityPerBox = ' +
document.labels.elements.frmMaxQuantityPerBox.valu e);
if (document.labels.elements.frmQuantityDelivered.val ue >
document.labels.elements.frmMaxQuantityPerBox.valu e)
{
alert('Stage 3');
document.labels.elements.frmLabelQty1.value ==
document.labels.elements.frmMaxQuantityPerBox.valu e;
}
else
{
alert('Stage 4');
alert(document.labels.elements.frmQuantityDelivere d.value /
document.labels.elements.frmMaxQuantityPerBox.valu e);
}
}

The values input are:
document.labels.elements.frmBundlesPerBox.value = 10
document.labels.elements.frmQuantityDelivered.valu e = 1000
document.labels.elements.frmMaxQuantityPerBox.valu e = 50

The problem occurs when "if
(document.labels.elements.frmQuantityDelivered.val ue >
document.labels.elements.frmMaxQuantityPerBox.valu e)" is evaluated.
The equation is supposedly calculating 1000>50, however it always
evaluates to false and displays the "Stage 4" alert. I know that it
is using the values of 1000 and 50 as "alert('frmQuantityDelivered = '
+ document.labels.elements.frmQuantityDelivered.valu e + ' -
frmMaxQuantityPerBox = ' +
document.labels.elements.frmMaxQuantityPerBox.valu e);" displays these
values.

I am guessing that I am missing something really obvious, but after
looking at the problem for a while, I am convinced that I am looking
past the problem now.

Cheers

ab

Feb 6 '07 #1
3 1523
Lee
andybeh said:
>
Hi All,

Can anyone tell me why the following code does not evaluate correctly
on my web page:
>The problem occurs when "if
(document.labels.elements.frmQuantityDelivered.va lue >
document.labels.elements.frmMaxQuantityPerBox.val ue)" is evaluated.
The equation is supposedly calculating 1000>50, however it always
evaluates to false and displays the "Stage 4" alert. I know that it
is using the values of 1000 and 50 as "alert('frmQuantityDelivered = '
+ document.labels.elements.frmQuantityDelivered.valu e + ' -
frmMaxQuantityPerBox = ' +
document.labels.elements.frmMaxQuantityPerBox.val ue);" displays these
values.

I am guessing that I am missing something really obvious, but after
looking at the problem for a while, I am convinced that I am looking
past the problem now.

The obvious thing you're missing is that form control values are
always strings, and the string "50" is greater than the string "1000".

if (+document.labels.elements.frmQuantityDelivered.va lue >
+document.labels.elements.frmMaxQuantityPerBox.val ue)

The unary + operator converts the strings to numbers.
--

Feb 6 '07 #2
Nice one, thanks heaps for that!

ab

Feb 6 '07 #3
In comp.lang.javascript message <11**********************@h3g2000cwc.goo
glegroups.com>, Tue, 6 Feb 2007 14:11:05, andybeh <el*****@hotmail.com>
posted:
>Can anyone tell me why the following code does not evaluate correctly
on my web page:
Many could.
>I am guessing that I am missing something really obvious, but after
looking at the problem for a while, I am convinced that I am looking
past the problem now.
You're probably really missing the FAQ, which is germane but not
explicit on the topic.

<FAQENTRY4.21 "Convert these to numbers before performing addition." -
add "or comparison".

It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 7 '07 #4

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

Similar topics

7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
3
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look...
36
by: Vivek Mandava | last post by:
Hello All, Which one is an expensive operation? '>' or '==" ? I know this won't matter much. However, if I'm executing this operation million times, I would prefer the better choice. My...
2
by: John Mark Howell | last post by:
BlankDoes anyone know how to overload the greater than (>) operator? I know that it is probably not in accordance to the framework design patterns, but it would make my code much cleaner. -- ...
7
by: BobRoyAce | last post by:
Let's say I have a text box on a page in which a user is to enter a monetary amount and that I want to ensure that the user enters a value greater than or equal to a certain value that will be...
5
by: PerlPhi | last post by:
hi,,, while ago i was wondering why do some programmers rarely uses the ternary operator. wherein it is less typing indeed. i believe in the classic virtue of Perl which is laziness. well let me show...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
3
by: sfrvn | last post by:
I have searched high and low and cannot find an answer to my problem. So now I turn to the collective genius of this newsgroup. Over-simplified examples This query criteria for field works:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.