...

View Full Version : need help with javascript



slang76
02-25-2007, 11:49 AM
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>

Mr J
02-25-2007, 12:34 PM
Appears to work if you remove the semi colons from the end of the if lines

Arty Effem
02-25-2007, 12:35 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum