PDA

View Full Version : what am I doing wrong on this form?



green9knight
07 Jan 2011, 01:32 PM
I am trying to adapt a script to work with a more complex problem than it was designed for. It is for my blog. Please help I cant get it to calculate.

<script language="JavaScript">
function calculate(Atext,Btext,Ctext,Dtext,E, form) {
E = [(Atext + Btext)- Ctext] / Dtext;}
return E;

var A = parseFloat(Atext);
var B = parseFloat(Btext);
var C = parseFloat(Ctext);
var D = parseFloat(Dtext);


form.Answer.value = E
}

/* ClearForm: this function has 1 argument: form.
It clears the input and answer fields on the form.
It needs to know the names of the INPUT elements in order
to do this. */

function ClearForm(form)
{
form.input_A.value = "";
form.input_B.value = "";
form.input_C.value = "";
form.input_D.value = "";
form.Answer.value = "";
}

// end of JavaScript functions -->
</script>




<form name="Calculator" method="post">
<p>Market Cap: <input type="TEXT" name="input_A" size="10" /></p>
<p>Debt: <input type="TEXT" name="input_B" size="10" /></p>
<p>Cash: <input type="TEXT" name="input_C" size="10" /></p>
<p>EBIT: <input type="TEXT" name="input_D" size="10" /></p>
<p><input type="button" value="Calculate Numbers" name="AddButton" onclick="CalculateSum(this.form.input_A.value, this.form.input_B.value,this.form.input_C.value,
this.form.input_D.value,)" /></p>
<p><input type="button" value="Clear Fields" name="ClearButton" onclick="ClearForm(this.form)" /></p>
<p>Answer = <input type="TEXT" name="Answer" size="12" /></p>
</form>

resdog
11 Jan 2011, 07:16 AM
There are several huge issues with the form...first, you call the function "calculate", but in the HTML form, you call the function "CalculateSum". so that won't work. Secondly, in the function, you have 5 "options" that need to be included (Atext,Btext,Ctext,Dtext,E, form), but when you call the function in the form, you only give 4 options.

Next, in the function itself, you end the function after the "E = [(Atext + Btext)- Ctext] / Dtext;" line, so the following code doesn't get executed, and doesn't output anything.

Also, you have things in the function that have no bearing on anything.



return E;


This does nothing, since you aren't accessing the function through a variable.



var A = parseFloat(Atext);
var B = parseFloat(Btext);
var C = parseFloat(Ctext);
var D = parseFloat(Dtext);


While this does something, it's not used anywhere else, so what's the point of this?

A quick scan of the code blocks shows a myriad of errors. Correct these and see if that doesn't help you.