Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: New to Java

  1. #1
    New Coder
    Join Date
    Apr 2003
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New Coder
    Join Date
    Apr 2003
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #5
    New Coder
    Join Date
    Apr 2003
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You should enter 3 numbers and it should give you an average. I enter in 2 4 6 and it gives me 82.

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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

  • #7
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •