View Full Version : New to Java

04-07-2003, 09:37 PM
I am new to java. I have made a small script that I feel should work but is not. Can some one take a look at it and tell me what is wrong? The script should average three numbers. When I click on the button to average it tells me that there is an error on the page. I can not find it but once again I am new to java.

<script language=JAVASCRIPT>

Function add() {
a = document.form1.a.value;
b = document.form1.b.value;
c = document.form1.c.value;
d = a+b+c;
e = d/3;
document.form1.total.value = e

<FORM name=form1>

<P><B>Average of Three Numbers</b></p>
<p>Enter First Number <input size=5 name=a></p>
<p>Enter Secound Number <input size=5 name=b></p>
<p>Enter Third Number <input size=5 name=c></p>
Results <input maxlength=40 size=5 name=total>
<input onclick=add() type=button value=Average>
<p><colspan=3><input type=reset value=Reset></p>

04-07-2003, 09:49 PM
You're writing Javascript, not Java. These 2 languages are very different and by NO means you can use Java instead of Javascript!

Your code doesn't work because F in Function is in capital. function is a keyword and you shouldn't write it in capital.

If you're new to JS, it's better that you write standard code. Don't use capital letters unless it's necessary, put the value of your form attributes in quotation and don't miss the type of an element simply because Text is the default type.

Keep on with the good work,

04-07-2003, 09:56 PM
I changed the function and now it generated a number. Not the correct one but a number none the less. Could you be a little more specific with "put the value of your form attributes in quotation" I am not sure what you are refereing to.

04-07-2003, 10:15 PM
Insteaf of writing

<input size=5 name=a>


<input type="text" size="5" name="a">

What do you enter that it doesn't answer as you expect?

04-07-2003, 10:18 PM
You should enter 3 numbers and it should give you an average. I enter in 2 4 6 and it gives me 82.

04-07-2003, 10:23 PM
That's because you used d=a+b+c on string values a="2", b="4", c="6", making d="246". "246" is then automagically transformed into the 246 then used in the division. Wrap the element values in parseInt([string ElementValue]) instead and you should be just fine.

04-07-2003, 10:25 PM
Well, first of all this is not Java, since Java and JavaScript have nothing in common except similar names.

Secondly, I would suggest putting quotes around all of your values. (i.e. <input size="5" name="a">) That might very well make it work.

Third, it's not "Function", it's "function". Note the lowercase "f" in function.

Hopefully this helps.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum