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

1. 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>

<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>

<body>

</body>
</html>

• Appears to work if you remove the semi colons from the end of the if lines

• Originally Posted by slang76
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
•