Newsgroup: it.comp.lang.javascript
Titolo: Stuck with a simple relational comparison!

Hello, I recently started learning Javascript but for some reason, I'm
completely stuck on this little problem. I'm creating two form boxes and
when 2 numbers are entered in the boxes, if the first number is larger than
the second, then the script should subtract the second number by the first
number. In both cases, the alert box displays the answer. If the first
number is less than the second number, the script multiplies both numbers.
If both are equal, an alert box displays a message.
What am I doing wrong? I keep on getting a few error codes and, the function
does not multiply!
Someone please help me!!! I'm sure that many of you could solve this in a
matter of seconds. I've been slaving away for 10hrs!!! I can't seem to find
the answer anywhere online. The code is below
Thanks,
MartyP
martin....@sympatico.ca
***********************************
<html>
<head>
<title>MartyP Assignment #6b</title>
<SCRIPT LANGUAGE="javascript">
<!-- function compare(document.form.first.value, document.form.second.value)
{  
 var f = document.form.first.value;
 var s = document.form.second.value;
 
// compare two numbers and subtract if first is greater than the second.
 
   if (f > s)   
   {subtract= f -s=true;
 alert("Since the first number is greater than the second, subtracting the second number from the first one equals"+subtract+".");
   document.form.first.value = "";
   document.form.second.value = "";
   }
 
// compare the two numbers and multiply if first is less than the second.  
 
 else if (f < s=true)
 {var multiply= f *s;
 alert("Since the first number was less than the second number, multiplying both numbers equals"+multiply);
 document.form.first.value = "";
 document.form.second.value = "";
 }
 
 else (f == s=true)
 { alert("Unfortunately, those two numbers happen to be equal!!! Better luck next time.")
 document.form.first.value = "";
 document.form.second.value = ""; }
}
-->
 </SCRIPT>
</head>
<body bgcolor="#99CCFF" text="#000080">
<p><font
face="Verdana" size="2"><b>Welcome to my Assignment #6b. Follow the
instructions for the
Javascript below.</b></font></p>
<p>&nbsp;</p>
<form name="form">
    <p align="center"><input type="text" name="first" size="20"></p>
  <p align="center"><input type="text" name="second" size="20"></p>
  <p align="center"><input type="button" value="Click to compare these two
numbers" onClick="compare(document.form.first.value,
document.form.second.value)"></p>
</form>
</body>
</html>

 the answer anywhere online. The code is below
There are many errors in your script. I could point them out here, but it
would take less time if you carefully compare the following (correct!)
script with yours.
<SCRIPT LANGUAGE="javascript">
<!--
function compare()
var f = parseInt(document.form.first.value);
var s = parseInt(document.form.second.value);
 if (f > s) {
  // compare two numbers and subtract if first is greater than the second.
  subtract = f-s;
  alert("Since the first number is greater than the second, subtracting the
second number from the first one equals"+subtract+".");
   } else if (f < s) {
  // compare the two numbers and multiply if first is less than the second.
  multiply = f*s;
  alert("Since the first number was less than the second number, multiplying
both numbers equals"+multiply);
  } else {
  alert("Unfortunately, those two numbers happen to be equal!!! Better luck
next time.")
 }
 document.form.first.value = "";
 document.form.second.value = "";
} //
-->
 </SCRIPT>
Another difference: the call to the function:
<INPUT TYPE="button" VALUE="Click to compare these two numbers"
onClick="compare()">
Some hints:
1) parseInt() is a function which converts a string to a number. It's
required, here, because values in text boxes are treated as strings by
default (and you should know that, for example, "12" < "3");
2) you don't need to specify "document.form.first.value" and
"document.form.second.value" as function parameters, since the function
itself takes these values in its first two rows;
3) an "if" condition is simply "if (a < b)" and not "if (a < b = true)",
which has no sense;
4) watch out for the brackets! One was missing.
5) Go on with your studies. Don't get discouraged!
Alx

 wrote in message
news:9afd85$pkn$2...@pegasus.tiscalinet.it...
