Subject: code not working
Posted By: Toka1 Post Date: 12/16/2003 6:30:33 AM
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

Reply By: planoie Reply Date: 12/16/2003 7:07:11 AM
The math doesn't look correct.  Why the extra * after 0.2253 in two places?
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.
Reply By: Toka1 Reply Date: 12/16/2003 8:37:03 AM
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.

Reply By: Toka1 Reply Date: 12/16/2003 8:43:12 AM
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 http-equiv=Content-Type content="text/html; charset=windows-1252">
<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

Reply By: pgtips Reply Date: 12/16/2003 8:53:06 AM
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 hard-coded the values of a and c.  Are these values of b which you are testing related to the hard-coded values of a and c?  If so, you may be better off going the whole-hog and making a and c variables, because once you do your "blue" bit of code will have to change again.

hth
Phil
Reply By: joefawcett Reply Date: 12/16/2003 9:26:07 AM
And what is
var b="-" + tonum(form.inputbox2.value);
?
Shouldn't it be
var b = (tonum(form.inputbox2.value)) * (-1);
?


Joe

--

Joe
Reply By: Toka1 Reply Date: 12/16/2003 9:58:48 AM
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


 






Reply By: Toka1 Reply Date: 12/16/2003 10:01:15 AM
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


Reply By: Toka1 Reply Date: 12/16/2003 10:18:17 AM
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

Reply By: joefawcett Reply Date: 12/16/2003 11:09:51 AM
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
Reply By: Toka1 Reply Date: 12/16/2003 11:31:37 AM
Thanks Joe

I will alter that, could you also tell me how I change a negative number to positive. Or an alternative to how I could code this.

var b="-" + tonum(form.inputbox2.value);


//Calculation of Discriminent
var d

if (b<=-6 && b>=-20)
{
    d=b*b-(4*0.2253*-194.4);
}
else if (b<=-21 && b>=-52)
{
    d=b*b-(4*0.2253*-134.4);
}
else if (b <-52)
{
    d= (b*4.635);
}


I will change the top line as you suggested.

In the above code for the first two conditions I need the input changing to a negative number.

But the last condition "d= (b*4.635);" I need b to stay as a positive input here.

So I need to now change this back to a positive number or write the negative line that you have helped me with to only correspond to the first two conditions.

Can you help at all

Thanks

Toka

Reply By: joefawcett Reply Date: 12/16/2003 11:47:25 AM
If I understand you correctly why not leave b as positive? As you are squaring it anyway it seems odd.
You could also calculate the fixed stuff. Assuming b is just the numeric value entered into the textbox:

if (b >= -6 && b <= -20)
{
  d=b*b-(4*0.2253*-194.4); //calculate 4*0.2253*-194.4 in advance here
}
else if (b >= 21 && b <= 52)
{
  d=b*b-(4*0.2253*-134.4);
}
else if (b > 52)
{
  d= (b*4.635);
}


--

Joe
Reply By: Toka1 Reply Date: 12/17/2003 4:08:22 AM
Hi Joe

I cannot leave b as positive or the calculation won't work.

The user will enter a positive number, to do the calculation in the first two instances it will only work on a negative number.

Therfore I need the input changing to a negative number for the first two conditions for the calculation to work and output the required result.

However the 3rd condition as the calculation is simpler works on the positive number input.

Calculating the fixed stuff is a great idea, Thanks



Go to topic 7672

Return to index page 985
Return to index page 984
Return to index page 983
Return to index page 982
Return to index page 981
Return to index page 980
Return to index page 979
Return to index page 978
Return to index page 977
Return to index page 976