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
    Feb 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help with javascript

    hi there,
    had to write a script for my studies and have a small problem with it.
    it is a conversion script, using arrays. it works fine, the only problem is, that when i want to convert from one currency to another, it outputs a conversion for all currencies and not just one. i think the problem is, that i used if statements, but am not to sure. can you please help???
    here is the complete script:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">
    <html>

    <head>
    <title></title>
    <script>
    var convertFrom, convertTo, amountEntered, amountConverted;
    var a = ['Dollar','Euro','Pound','Yen'];
    var b = ['(USD)','(EUR)','(GBP)','(JPY)'];
    var c = ['1','1.27','1.87','0.0085'];

    convertFrom = window.prompt('Please enter currency to convert from: 0 Dollar 1 Euro 2 Pound 3 Yen','');
    convertFrom = parseFloat(convertFrom);

    convertTo = window.prompt('Please enter currency to convert to: 0 Dollar 1 Euro 2 Pound 3 Yen','');
    convertTo = parseFloat(convertTo);

    amountEntered = window.prompt('Please enter amount to be converted','')
    amountEntered = parseFloat(amountEntered);

    //convert Dollar to Euro

    if(convertFrom == 0 && convertTo == 1);
    {
    amountConverted = amountEntered / c[1];
    document.write(amountEntered + ' ' + a[0] + ' ' + b[0] + ' ' + 'are ' + amountConverted + ' '+ b[1] + ' ' + a[1],'<br>');
    };

    //convert Euro to Pound

    if(convertFrom == 1 && convertTo == 2);
    {

    amountConverted = amountEntered * c[1] / c[2];
    document.write(amountEntered + ' ' + a[1] + ' ' + b[1] + ' ' + 'are ' + amountConverted + ' '+ b[2] + ' ' + a[2],'<br>');
    };

    //convert Pound to Euro

    if(convertFrom == 2 && convertTo == 1);
    {
    amountConverted = amountEntered * c[2] / c[1];
    document.write(amountEntered + ' ' + a[2] + ' ' + b[2] + ' ' + 'are ' + amountConverted + ' '+ b[1] + ' ' + a[1],'<br>');
    };

    //convert Yen to Pound

    if(convertFrom == 3 && convertTo == 2);
    {
    amountConverted = amountEntered * c[3] / c[2];
    document.write(amountEntered + ' ' + a[3] + ' ' + b[3] + ' ' + 'are ' + amountConverted + ' '+ b[2] + ' ' + a[2],'<br>');
    };




    </script>







    </head>
    <body>


    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Appears to work if you remove the semi colons from the end of the if lines
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by slang76 View Post
    hi there,
    had to write a script for my studies and have a small problem with it.
    it is a conversion script, using arrays. it works fine, the only problem is, that when i want to convert from one currency to another, it outputs a conversion for all currencies and not just one. i think the problem is, that i used if statements, but am not to sure. can you please help???
    if(convertFrom == 0 && convertTo == 1);
    Lose the semicolons after the if statements.


  •  

    Posting Permissions

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