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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2006
    Location
    Canberra, Australia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problems with the Euro Calculator

    I've been learning JavaScript for a two months now, however I have learnt quite a lot. However I've had difficulty with an example from a book called "Eways - Javascript for Beginners". The exercise is a euro calculator in which you simply enter the amount in the old Francs etc. and it converts the currency. Simply however, there nothing happens.

    Here is the code script:
    [INDENT]
    <script language="text/javascript">
    var a=0
    function calculate()

    if (a==0)
    {
    document.forms[0].euro.value=Math.round(document.forms[0].amount.value / document.forms[0].currency[document.forms[0].currency.selectedIndex].value * 100 / 100
    }

    if (a==1)
    {
    document.forms[0].amount.value=Math.round(document.forms[0].euro.value * document.forms[0].currency[document.forms[0].currency.selectedIndex].value * 100 / 100
    }
    </script>




    And body code:

    <input name="amount" size="10" onChange="a=0">
    <select name="currency" onChange="calculate()" onFocus="calculate()">
    <option value="13.7603">ATS</option>
    <option value="40.3399">BEF/LUF</option>
    <option value="1.95583">DEM</option>
    <option value="166.386">ESP</option>
    <option value="5.94573">FIM</option>
    <option value="6.55957">FRF</option>
    <option value="0.787564">IEP</option>
    <option value="1936.27">ITL</option>
    <option value="2.20371">NLG</option>
    <option value="200.482">PTE</option>
    </select>
    corresponds to
    <input name="euro" size="10" onChange="a=1">
    Euros.<br>
    <input type="button" value="calculate" onClick="calculate()">

    </INDENT>

    Thanks, any help will be appreciated. I am surprised why its not working cause it's from a published book.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,480
    Thanks
    0
    Thanked 635 Times in 625 Posts
    You are missing a ) on the end of each of the calculations to close the Math.round(...)

    function calculate()

    if (!a)
    {
    document.forms[0].euro.value=Math.round(document.forms[0].amount.value / document.forms[0].currency[document.forms[0].currency.selectedIndex].value * 100) / 100;
    }
    else
    {
    document.forms[0].amount.value=Math.round(document.forms[0].euro.value * document.forms[0].currency[document.forms[0].currency.selectedIndex].value * 100) / 100;
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New to the CF scene
    Join Date
    Aug 2006
    Location
    Canberra, Australia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Thanks for the help

    You're a lifesaver. I've been so confused. I've even parsed the code in Notepad++ to no avail. Thankyou for the help. I also checked out your web site. It's very informative and I'll use it a reference when I strike trouble in the future. Cheers


  •  

    Posting Permissions

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