Creating a currency converter using three parrallel arrays | Newbie | | Join Date: Feb 2007
Posts: 3
| | |
Hi, I'm hoping someone can help me I am quite new to Javascript and have had to create a programme which converts dollars into euros and vice versa, here is my script which is working
var currency;
var amountEntered;
currency = window.prompt('Please enter 0 to convert from Dollars to euros and 1 to convert form euros to dollars','');
currency = parseFloat(currency);
while (currency < 0 || currency > 1)
{
currency = window.prompt('Please re enter currency should be 0 or 1','');
currency = parseFloat(currency);
}
amountEntered = window.prompt('Please enter the amount to be converted','');
amountEntered = parseFloat(amountEntered);
if (currency == 0 )
{
document.write('<BR>' + amountEntered + ' Dollars converts to ' + amountEntered / 1.27 + ' Euros')
currency = parseFloat (currency);
}
else
{
document.write('<BR>' + amountEntered + ' Euros converts to ' + amountEntered * 1.27 + ' Dollars')
}
I now have to expand this to cope flexibly with four currencies
I need to prompt the user to input a number indicating a currency to convert from (0 for dollars, 1 for euros, 2 for pounds and 3 for yen)
then prompt the user to input a number indicating the currency to convert to using the same numbers as above to represent the 4 currencies
after this the user needs to enter the amount to be converted
then perform the coversion and write the result to the screen including the currency codes (USD, EUR, GBP, JPY)
the programme should be able to convert between any two currencies and I have to use three parallel arrays to store the currency names, codes and conversion rates,
The programme also has to convert to dollars first then into the target currency
conversion rates are
dollar = 1 dallar
euro = 1.27 dollars
pound = 1.87
yen = 0.0085 dollars
I have really struggled with this and spent a lot of time trying to work out the best way to go about it, I am now at my wits end, if anyone can help with tips or some guidance it really will be appreciated
|  | Forum Leader | | Join Date: Jul 2006 Location: Oklahoma
Posts: 1,076
| | | re: Creating a currency converter using three parrallel arrays
after looking at google it seems as if your calculations to convert to usd is a bit off. 1 Euro = 1.3081 U.S. dollar 1 Japanese yen = 0.00826036676 U.S. dollar 1 British pound = 1.9539 U.S. dollar I left the code with your calculations in it. if you need help or even want these calculations in it let me know and i will help u put them in |  | Forum Leader | | Join Date: Jul 2006 Location: Oklahoma
Posts: 1,076
| | | re: Creating a currency converter using three parrallel arrays -
<html>
-
<script>
-
function ConvertToDollar(val, from) {
-
if (from == 0) { response = val; }
-
if (from == 1) { response = roundNumber(val/1.3081,2); }
-
if (from == 2) { response = roundNumber(val/1.9539,2); }
-
if (from == 3) { response = roundNumber(val/0.00826036676,2); }
-
return response;
-
}
-
function ConvertCurrency(from, to, val, textT, textF) {
-
var response = "";
-
-
newcurrency = ConvertToDollar(val, from);
-
if (to == 1) { newcurrency = roundNumber(newcurrency*1.3081,2); }
-
if (to == 2) { newcurrency = roundNumber(newcurrency*1.9539,2); }
-
if (to == 3) { newcurrency = roundNumber(newcurrency*0.00826036676,2); }
-
response = val+"<b> "+textF+" = </b>"+newcurrency+"<b> "+textT+"</b>";
-
document.getElementById("showcurrency").innerHTML = response;
-
}
-
function roundNumber(num, dec) {
-
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
-
return result;
-
}
-
</script>
-
<b>Convert </b><input type="text" value="0.00" id="currency">
-
<b>From </b>
-
<select id="from">
-
<option value="0">USD</option>
-
<option value="1">Euro</option>
-
<option value="2">Pound</option>
-
<option value="3">Yen</option>
-
</select>
-
<b>To </b>
-
<select id="to">
-
<option value="0">USD</option>
-
<option value="1">Euro</option>
-
<option value="2">Pound</option>
-
<option value="3">Yen</option>
-
</select>
-
<input type="button" value="Convert" onclick="ConvertCurrency(document.getElementById('from').value, document.getElementById('to').value, document.getElementById('currency').value, document.getElementById('to').options[document.getElementById('to').selectedIndex].text, document.getElementById('from').options[document.getElementById('from').selectedIndex].text);">
-
<br>
-
<div id="showcurrency"></div>
-
</html>
-
My calculations are off unless your calculating from usd
theres you a start though
| | Needs Regular Fix | | Join Date: Jun 2006
Posts: 424
| | | re: Creating a currency converter using three parrallel arrays
You could use an object.
When you create an instance, set the unique properties- amount, currency format, and date. Set the conversion methods (to and from) with arrays of exchange ratesin the object's prototype.
This makes it trivial to add new currencies, or update the exchange rates- (which is why you need a date attached to every transaction).
| | Newbie | | Join Date: Mar 2007
Posts: 1
| | | re: Creating a currency converter using three parrallel arrays Quote:
Originally Posted by iam_clint -
<html>
-
<script>
-
function ConvertToDollar(val, from) {
-
if (from == 0) { response = val; }
-
if (from == 1) { response = roundNumber(val/1.3081,2); }
-
if (from == 2) { response = roundNumber(val/1.9539,2); }
-
if (from == 3) { response = roundNumber(val/0.00826036676,2); }
-
return response;
-
}
-
function ConvertCurrency(from, to, val, textT, textF) {
-
var response = "";
-
-
newcurrency = ConvertToDollar(val, from);
-
if (to == 1) { newcurrency = roundNumber(newcurrency*1.3081,2); }
-
if (to == 2) { newcurrency = roundNumber(newcurrency*1.9539,2); }
-
if (to == 3) { newcurrency = roundNumber(newcurrency*0.00826036676,2); }
-
response = val+"<b> "+textF+" = </b>"+newcurrency+"<b> "+textT+"</b>";
-
document.getElementById("showcurrency").innerHTML = response;
-
}
-
function roundNumber(num, dec) {
-
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
-
return result;
-
}
-
</script>
-
<b>Convert </b><input type="text" value="0.00" id="currency">
-
<b>From </b>
-
<select id="from">
-
<option value="0">USD</option>
-
<option value="1">Euro</option>
-
<option value="2">Pound</option>
-
<option value="3">Yen</option>
-
</select>
-
<b>To </b>
-
<select id="to">
-
<option value="0">USD</option>
-
<option value="1">Euro</option>
-
<option value="2">Pound</option>
-
<option value="3">Yen</option>
-
</select>
-
<input type="button" value="Convert" onclick="ConvertCurrency(document.getElementById('from').value, document.getElementById('to').value, document.getElementById('currency').value, document.getElementById('to').options[document.getElementById('to').selectedIndex].text, document.getElementById('from').options[document.getElementById('from').selectedIndex].text);">
-
<br>
-
<div id="showcurrency"></div>
-
</html>
-
My calculations are off unless your calculating from usd
theres you a start though Hi there?
I too need help?
I too have to create a currency converter like this one. I wondered if you could alter this in a way to use window.prompts for currency conversion rather than the form?
I thank you in advance for any help.
Lors
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Creating a currency converter using three parrallel arrays Quote:
Originally Posted by lors Hi there?
I too need help?
I too have to create a currency converter like this one. I wondered if you could alter this in a way to use window.prompts for currency conversion rather than the form?
I thank you in advance for any help.
Lors See this page for an example of prompt. Use the variable instead of the form, then pass these values into the currency conversion function.
| | Newbie | | Join Date: Mar 2007
Posts: 3
| | | re: Creating a currency converter using three parrallel arrays
Hi,
I'm completely confused, I need to adapt a currency converter to convert between four specific currencies. Using the conversion rates of Dollar = 1 dollar,
Euro = 1.27 dollars, Pound Sterling = 1.87 dollars, Japanese = 0.0085 dollars.
I need to use three parallel arrays to store the currency names, codes and conversion rates and also window prompts for these three points:
1. prompt the user to input a number indicating a currency to convert from ( 0 dollars, 1 for euros, 2 for pounds and 3 for yen.
2. prompt the user to input a number indicating a currency to convert to.
3. prompt the user to input the amount to be converted.
but the converter then also needs to perform a conversion & write the result to the screen including the names of the currencies and their three letter codes Dollar (USD), Euro (EUR), Pound Sterling (GBP) and Japanese Yen (JPY).
Any help you can give would be most apprieciated.
Laura
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Creating a currency converter using three parrallel arrays Quote:
Originally Posted by LBailey Hi,
I'm completely confused, I need to adapt a currency converter to convert between four specific currencies. Using the conversion rates of Dollar = 1 dollar,
Euro = 1.27 dollars, Pound Sterling = 1.87 dollars, Japanese = 0.0085 dollars.
I need to use three parallel arrays to store the currency names, codes and conversion rates and also window prompts for these three points:
1. prompt the user to input a number indicating a currency to convert from ( 0 dollars, 1 for euros, 2 for pounds and 3 for yen.
2. prompt the user to input a number indicating a currency to convert to.
3. prompt the user to input the amount to be converted.
but the converter then also needs to perform a conversion & write the result to the screen including the names of the currencies and their three letter codes Dollar (USD), Euro (EUR), Pound Sterling (GBP) and Japanese Yen (JPY).
Any help you can give would be most apprieciated.
Laura Hmm... two people (or maybe three) with the same problem. Sounds like homework/class assignment to me.
What gives it away is "three parallel arrays" and the same currency exchange rates.
So, I am going to follow standard procedures which you will see in the next post...
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Creating a currency converter using three parrallel arrays
The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.
Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.
Then when you are ready post a new question in this thread.
MODERATOR
|  | Forum Leader | | Join Date: Jul 2006 Location: Oklahoma
Posts: 1,076
| | | re: Creating a currency converter using three parrallel arrays
Parrallel Arrays!!!!
var p = new Array();
p[0] = "USD";
p[1] = "YEN";
var b = new Array();
b[0] = 0;
b[1] = 1;
oh no.
| | Newbie | | Join Date: Mar 2007
Posts: 3
| | | re: Creating a currency converter using three parrallel arrays
I think i have a solution, if i replaced this part of the code mentioned above (*) with the window.prompt code would the converter still work?
(*) function ConvertToDollar(val, from) {
Laura
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Creating a currency converter using three parrallel arrays Quote:
Originally Posted by LBailey I think i have a solution, if i replaced this part of the code mentioned above (*) with the window.prompt code would the converter still work?
(*) function ConvertToDollar(val, from) {
Laura No, it wouldn't. You need to take the return values from the prompt, validate them (in case they're invalid) and then pass them to the ConvertToDollar function.
| | Newbie | | Join Date: Mar 2007
Posts: 3
| | | re: Creating a currency converter using three parrallel arrays
Hi,
I think i've almost cracked it, i'm now just stuck on the equation at the bottom of the code. Any help you could give would be appreciated, Regards, Laura.
So far i have: Removed code - moderator - dollarAmount = amountEntered * currencyRates[sourceCurrency];
-
amountConverted = dollarAmount / currencyRates[targetCurrency];
-
-
document.write('<BR>' + amountEntered + currencyNames[sourceCurrency] ' + ' currencyCodes[sourceCurrency] + ') ' + ' is ' + amountConverted + ' ' +
-
currencyNames[targetCurrency] + ' (' + currencyCodes[targetCurrency] + ') ');
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Creating a currency converter using three parrallel arrays
You're going to get errors because you've not used quotes and strings correctly.
Try: - document.write('<BR>' + amountEntered + ' ' + currencyNames[sourceCurrency] + ' (' + currencyCodes[sourceCurrency] + ') ' + ' is ' + amountConverted + ' ' +
-
currencyNames[targetCurrency] + ' (' + currencyCodes[targetCurrency] + ') ');
| | Newbie | | Join Date: Mar 2007
Posts: 2
| | | re: Creating a currency converter using three parrallel arrays
Hello...
Yo are right...this is kind of assignment and Im struggling as well, but I`m not going to ask you for code...my question is - just give me hint how I can use three paralel arrays to make currency conversion program...?
I really am a complete beginner and little hint might help me to understand what I did not get yet...
|  | Forum Leader | | Join Date: Jul 2006 Location: Oklahoma
Posts: 1,076
| | | re: Creating a currency converter using three parrallel arrays
personally i wouldn't use 3 parralel arrays
infact i probably wouldn't use an array at all
i don't understand why they are trying to make you guys do parralel arrays to accomplish this
| | Newbie | | Join Date: Mar 2007
Posts: 2
| | | re: Creating a currency converter using three parrallel arrays Quote:
Originally Posted by iam_clint personally i wouldn't use 3 parralel arrays
infact i probably wouldn't use an array at all
i don't understand why they are trying to make you guys do parralel arrays to accomplish this
That`s what I mean...I am just begginer, but can`t see any point using arrays.But anyway....some tip might help...
|  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,533 network members.
|