
Javascript HowTo Ask your "How do I do this with Javascript?" questions here. 
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Javascript HowTo section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com



December 16th, 2003, 07:30 AM

Authorized User


Join Date: Nov 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts


code not working
Hi
I have this piece of code that was working fine. Until I added the blue piece of code in. It is now not working any pointers in the right direction would be gratefully recieved.
<HTML><HEAD><TITLE>Quadratic Equation</TITLE>
<SCRIPT language=JavaScript>
//Function to Convert string to number
function tonum(obj)
{
return parseFloat(obj);
}
//This Function is called when the "Solve" button is pressed
//It again calls the "solve_quad" function
function inputtest(form, button)
{
solve_quad(form);
return;
}
//Function  Solving of Quadratic Equation
function solve_quad(form)
{
//Conversion of all Input values to Numbers
var b="" + tonum(form.inputbox2.value);
//Calculation of Discriminent
var d
if (b>=6 &&<=20)
{
d=b*b(4*0.2253*194.4);
}
else if (b>=21 &&<=52)
{
d=b*b(4*0.2253*134.4);
}
else if (b >52)
{
d= (b*4.635);
}
if(d<0)
{
var e=Math.sqrt(d);
var neg=true;
}
else
{
var e=Math.sqrt(d);
var neg=false;
}
//Calculating Real and Img parts
var f=b/(2*0.2253);
var g=e/(2*0.2253);
if(neg)
{
form.outputbox1.value=f+"+"+g+"i";
}
else
{
form.outputbox1.value=f+g;
}
return;
}
function clearForm(form) {
form.inputbox2.value="";
}
</SCRIPT>
<META content="MSHTML 5.50.4919.2200" name=GENERATOR></HEAD>
<BODY>
<FORM method=post>
<BLOCKQUOTE>
<P>This program solves Quadratic Equations. Enter the coefficients in the
appropriate boxes and then click <STRONG>Solve</STRONG>. The results will
appear in the boxes labeled <STRONG>Root 1</STRONG> and <STRONG>Root
2</STRONG>. For example, for the quadratic equation below, you would enter 1,
5 and 6 as your coefficients. After pressing <STRONG>Solve</STRONG>, your
resulting roots would be 2 and 3.<BR></P></BLOCKQUOTE>
<DIV align=center>
<CENTER>
<P>X<SUP>2</SUP> + 5X + 6 = 0</P></CENTER></DIV>
<DIV align=center>
<CENTER>
<TABLE cellSpacing=0 cellPadding=0 bgColor=#00ffff border=5>
<TBODY>
<TR>
<TD>Enter the Coefficient of X here</TD>
<TD><INPUT size=5 name=inputbox2></TD></TR>
</TD></TR></TBODY></TABLE></CENTER></DIV>
<DIV align=center>
<CENTER>
<P><BR>Root 1: <INPUT size=30 name=outputbox1><BR></P></CENTER></DIV>
<DIV align=center>
<CENTER>
<P><INPUT onclick=inputtest(this.form,this) type=button value=Solve name=C2F> <INPUT onclick=clearForm(this.form) type=reset value=Reset name=second></P></CENTER></DIV></FORM></BODY></HTML>
Thank you for any help given
Toka

December 16th, 2003, 08:07 AM


Friend of Wrox


Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts


The math doesn't look correct. Why the extra * after 0.2253 in two places?
Code:
if (b>=6 &&<=20)
{
d=b*b(4*0.2253*194.4);
//^
}
else if (b>=21 &&<=52)
{
d=b*b(4*0.2253*134.4);
//^
}
else if (b >52)
{
d= (b*4.635);
}
Peter

Work smarter, not harder.

December 16th, 2003, 09:37 AM

Authorized User


Join Date: Nov 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts


Hi
It is meant to be *
d=b*b(4*0.2253*194.4);
d = b times b minus (4 times 0.22.53 times negative 194.4)
This is the correct maths for a quadratic equation.
But there is a problem with my coding somewhere, if anyone could help please.

December 16th, 2003, 09:43 AM

Authorized User


Join Date: Nov 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts


If it helps, this is the full script working before I put the extra conditions in, as in the blue code in the above sample
<HTML><HEAD><TITLE>Quadratic Equation</TITLE>
<META httpequiv=ContentType content="text/html; charset=windows1252">
<SCRIPT language=JavaScript>
//Function to Convert string to number
function tonum(obj)
{
return parseFloat(obj);
}
//This Function is called when the "Solve" button is pressed
//It again calls the "solve_quad" function
function inputtest(form, button)
{
solve_quad(form);
return;
}
//Function  Solving of Quadratic Equation
function solve_quad(form)
{
//Conversion of all Input values to Numbers
var b="" + tonum(form.inputbox2.value);
//Calculation of Discriminent
var d=b*b(4*0.2253*194.4);
if(d<0)
{
var e=Math.sqrt(d);
var neg=true;
}
else
{
var e=Math.sqrt(d);
var neg=false;
}
//Calculating Real and Img parts
var f=b/(2*0.2253);
var g=e/(2*0.2253);
if(neg)
{
form.outputbox1.value=f+"+"+g+"i";
}
else
{
form.outputbox1.value=f+g;
}
return;
}
function clearForm(form) {
form.inputbox2.value="";
}
</SCRIPT>
<META content="MSHTML 5.50.4919.2200" name=GENERATOR></HEAD>
<BODY>
<FORM method=post>
<BLOCKQUOTE>
<P>This program solves Quadratic Equations. Enter the coefficients in the
appropriate boxes and then click <STRONG>Solve</STRONG>. The results will
appear in the boxes labeled <STRONG>Root 1</STRONG> and <STRONG>Root
2</STRONG>. For example, for the quadratic equation below, you would enter 1,
5 and 6 as your coefficients. After pressing <STRONG>Solve</STRONG>, your
resulting roots would be 2 and 3.<BR></P></BLOCKQUOTE>
<DIV align=center>
<CENTER>
<P>X<SUP>2</SUP> + 5X + 6 = 0</P></CENTER></DIV>
<DIV align=center>
<CENTER>
<TABLE cellSpacing=0 cellPadding=0 bgColor=#00ffff border=5>
<TBODY>
<TR>
<TD>Enter the Coefficient of X here</TD>
<TD><INPUT size=5 name=inputbox2></TD></TR>
</TD></TR></TBODY></TABLE></CENTER></DIV>
<DIV align=center>
<CENTER>
<P><BR>Root 1: <INPUT size=30 name=outputbox1><BR></P></CENTER></DIV>
<DIV align=center>
<CENTER>
<P><INPUT onclick=inputtest(this.form,this) type=button value=Solve name=C2F> <INPUT onclick=clearForm(this.form) type=reset value=Reset name=second></P></CENTER></DIV></FORM></BODY></HTML>
This works fine, but I need slightly different formula's for different range of input as above.
When I add this my code stops working, I am obviously missing something.
Would be grateful of any help to solve this issue
Thanks

December 16th, 2003, 09:53 AM

Friend of Wrox


Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post


There is a syntax problem in your 'blue' code for sure:
if (b>=6 &&<=20)
you are missing the variable in the 2nd condition, if it should also be b then change this line to:
if (b>=6 && b<=20)
but, that will just result in a condition that is never met. A number cannot be both >=6 and <=20.
The same goes for the 2nd test
else if (b>=21 &&<=52)
On a side issue, how useful is this bit of code you've added? I see you're calculating the discriminant, but you've hardcoded the values of a and c. Are these values of b which you are testing related to the hardcoded values of a and c? If so, you may be better off going the wholehog and making a and c variables, because once you do your "blue" bit of code will have to change again.
hth
Phil

December 16th, 2003, 10:26 AM


Wrox Author


Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts


And what is
Code:
var b="" + tonum(form.inputbox2.value);
?
Shouldn't it be
Code:
var b = (tonum(form.inputbox2.value)) * (1);
?
Joe

Joe

December 16th, 2003, 10:58 AM

Authorized User


Join Date: Nov 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts


Hi Phil
On the first issue it should be "b" on both sides thank you I had missed a variable out.
As for this line:
but, that will just result in a condition that is never met. A number cannot be both >=6 and <=20.
I thought that this means greater than or equal to 6 or less than or equal to 20 ie: between minus 6 and 20 inclusive. Have I got this wrong??
I have hard coded the a & c as these are set in stone for the different conditions and will not need to change.
for 6 to 20 a & c will always be the same it is just the input "b" which will change.
for 21 to 52 a & c will always be as in blue code just "b will change.
and the same for over 52.
I would of used variables for a & c if they stayed the same and there were no conditions.
Is this not the right way to do it.
sorry all this is quite new to me, I am a novice, although you have probably gathered that already.
thanks for info
Toka

December 16th, 2003, 11:01 AM

Authorized User


Join Date: Nov 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts


Hi Joe
var b="" + tonum(form.inputbox2.value);
The input will be a positive number this is to convert it to a negative number.
It seems to work, have I got this wrong also??.
Toka

December 16th, 2003, 11:18 AM

Authorized User


Join Date: Nov 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts


Hi Phil
Stupid me, I have just realised, I have got the <&> symbols the wrong way round.
I have changed them and all seems to be working fine, although I have only looked at it quickly no doubt I will find more errors as I progress
Thank a Lot for all your help
Toka

December 16th, 2003, 12:09 PM


Wrox Author


Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts


Quote:
quote:Originally posted by Toka1
Hi Joe
var b="" + tonum(form.inputbox2.value);
The input will be a positive number this is to convert it to a negative number.
It seems to work, have I got this wrong also??.

Yes, you are creating a string equal to, if b equals 6 for instance, "6". This is getting converted to 6 later but in an unreliable fashion. As you have already converted the input to a floating number just multiply by 1 to get the negative.

Joe


