Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-07-2003, 08:37 PM   PM User | #1
Twister
New Coder

 
Join Date: Apr 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Twister is an unknown quantity at this point
New to Java

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
}
//-->
</script>

<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>
Twister is offline   Reply With Quote
Old 04-07-2003, 08:49 PM   PM User | #2
ConfusedOfLife
Regular Coder

 
Join Date: Jul 2002
Location: Iran
Posts: 695
Thanks: 0
Thanked 0 Times in 0 Posts
ConfusedOfLife is an unknown quantity at this point
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,
bijan
ConfusedOfLife is offline   Reply With Quote
Old 04-07-2003, 08:56 PM   PM User | #3
Twister
New Coder

 
Join Date: Apr 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Twister is an unknown quantity at this point
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.
Twister is offline   Reply With Quote
Old 04-07-2003, 09:15 PM   PM User | #4
ConfusedOfLife
Regular Coder

 
Join Date: Jul 2002
Location: Iran
Posts: 695
Thanks: 0
Thanked 0 Times in 0 Posts
ConfusedOfLife is an unknown quantity at this point
Insteaf of writing
Code:
<input size=5 name=a>
write:
Code:
<input type="text" size="5" name="a">
What do you enter that it doesn't answer as you expect?
ConfusedOfLife is offline   Reply With Quote
Old 04-07-2003, 09:18 PM   PM User | #5
Twister
New Coder

 
Join Date: Apr 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Twister is an unknown quantity at this point
You should enter 3 numbers and it should give you an average. I enter in 2 4 6 and it gives me 82.
Twister is offline   Reply With Quote
Old 04-07-2003, 09:23 PM   PM User | #6
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
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.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 04-07-2003, 09:25 PM   PM User | #7
Skyzyx
Regular Coder

 
Skyzyx's Avatar
 
Join Date: Aug 2002
Location: Silicon Valley, CA
Posts: 980
Thanks: 0
Thanked 0 Times in 0 Posts
Skyzyx is on a distinguished road
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.
__________________

Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.
Skyzyx is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:18 PM.


Advertisement
Log in to turn off these ads.