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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow Simple Calculator - I've hit a wall..

    Hey,

    This is my first post!

    I'm trying to learn javascript and i have tried to create a simple calculator, I have a calculator that does
    Code:
     x * x = x
    but when i try to cut / paste it into another project it just refuses to work!
    This is undoubtably VERY BASIC but any help is VERY much appreciated

    Code:
     
    
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin 
    function fmtPrice(value) {
    result="$"+Math.floor(value)+".";
    var cents=100*(value-Math.floor(value))+0.5;
    result += Math.floor(cents/10);
    result += Math.floor(cents%10);
    return result;
    }
    function compute() {
    var total_price = (document.forms[0].cost.value)*(document.forms[0].qty.value);
    document.forms[0].total_price.value=total_price;
    var total_price = fmtPrice(total_price);
    document.forms[0].total_price.value=total_price;
    }
    function resetIt() {
    document.forms[0].cost.value="0";
    document.forms[0].qty.value="0";
    document.forms[0].total_price.value="";
    }
    // End -->
    </SCRIPT>
    Here is a link to the page - http://dl.dropbox.com/u/29087/Java%20Laptop%20Calc.html
    ( so you can view source code, saving me from posting it all here )

    what am i doing wrong?!! man java is so much harder than VB!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

    <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead. The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

    x * x = x is not valid syntax. It should be x = x * x.


    Finally, do not use the same name (such as total_price) for an HTML element and a Javascript variable.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 11-14-2010 at 09:16 AM.

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post

    Finally, do not use the same name (such as total_price) for an HTML element and a Javascript variable.
    ok, so if i have a textbox that is

    Code:
     <input type="text" name="total_price" size="15" />
    and in the header im calling it total_price this is bad? what do i need to do instead?
    Ie.
    Code:
    document.forms[0].<b>total_price</b>.value
    Forgive me, i am completely new to this and in all honesty i think my brain is just wired in a way which refuses to comprehend it.

    if you know of any good books / information that i should go read i'd love to hear of them, the tutorials i've found on the internet so far have done nothing but confuse me further.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    There is nothing wrong with total_price as such.

    But you are using the same name as both a Javascript variable and an HTML element.

    document.forms[0].total_price.value=total_price;

    Apart from being confusing, that can cause big problems in IE.
    JScript (Internet Explorer's equivalent to JavaScript) automatically maps HTML fields with names and ids to the equivalent variable in JScript so you need to avoid using the same name in both places. This is one of the most common reasons for scripts, that run quite nicely in most web browsers, to malfunction when you test them in Internet Explorer.


  •  

    Tags for this Thread

    Posting Permissions

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