Connecting Tech Pros Worldwide Forums | Help | Site Map

Creating a currency converter using three parrallel arrays

Newbie
 
Join Date: Feb 2007
Posts: 3
#1: Feb 15 '07
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

iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#2: Feb 15 '07

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
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#3: Feb 15 '07

re: Creating a currency converter using three parrallel arrays


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <script>
  3. function ConvertToDollar(val, from) {
  4. if (from == 0) { response = val; }
  5. if (from == 1) { response = roundNumber(val/1.3081,2); }
  6. if (from == 2) { response = roundNumber(val/1.9539,2); }
  7. if (from == 3) { response = roundNumber(val/0.00826036676,2); }
  8. return response;
  9. }
  10. function ConvertCurrency(from, to, val, textT, textF) {
  11. var response = "";
  12.  
  13. newcurrency = ConvertToDollar(val, from);
  14. if (to == 1) { newcurrency = roundNumber(newcurrency*1.3081,2); }
  15. if (to == 2) { newcurrency = roundNumber(newcurrency*1.9539,2); }
  16. if (to == 3) { newcurrency = roundNumber(newcurrency*0.00826036676,2); }
  17. response = val+"<b> "+textF+" = </b>"+newcurrency+"<b> "+textT+"</b>";
  18. document.getElementById("showcurrency").innerHTML = response;
  19. }
  20. function roundNumber(num, dec) {
  21. var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
  22. return result;
  23. }
  24. </script>
  25. <b>Convert </b><input type="text" value="0.00" id="currency">
  26. <b>From </b>
  27. <select id="from">
  28. <option value="0">USD</option>
  29. <option value="1">Euro</option>
  30. <option value="2">Pound</option>
  31. <option value="3">Yen</option>
  32. </select>
  33. <b>To </b>
  34. <select id="to">
  35. <option value="0">USD</option>
  36. <option value="1">Euro</option>
  37. <option value="2">Pound</option>
  38. <option value="3">Yen</option>
  39. </select>
  40. <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);">
  41. <br>
  42. <div id="showcurrency"></div>
  43. </html>
  44.  

My calculations are off unless your calculating from usd

theres you a start though
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#4: Feb 16 '07

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
#5: Mar 3 '07

re: Creating a currency converter using three parrallel arrays


Quote:

Originally Posted by iam_clint

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <script>
  3. function ConvertToDollar(val, from) {
  4. if (from == 0) { response = val; }
  5. if (from == 1) { response = roundNumber(val/1.3081,2); }
  6. if (from == 2) { response = roundNumber(val/1.9539,2); }
  7. if (from == 3) { response = roundNumber(val/0.00826036676,2); }
  8. return response;
  9. }
  10. function ConvertCurrency(from, to, val, textT, textF) {
  11. var response = "";
  12.  
  13. newcurrency = ConvertToDollar(val, from);
  14. if (to == 1) { newcurrency = roundNumber(newcurrency*1.3081,2); }
  15. if (to == 2) { newcurrency = roundNumber(newcurrency*1.9539,2); }
  16. if (to == 3) { newcurrency = roundNumber(newcurrency*0.00826036676,2); }
  17. response = val+"<b> "+textF+" = </b>"+newcurrency+"<b> "+textT+"</b>";
  18. document.getElementById("showcurrency").innerHTML = response;
  19. }
  20. function roundNumber(num, dec) {
  21. var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
  22. return result;
  23. }
  24. </script>
  25. <b>Convert </b><input type="text" value="0.00" id="currency">
  26. <b>From </b>
  27. <select id="from">
  28. <option value="0">USD</option>
  29. <option value="1">Euro</option>
  30. <option value="2">Pound</option>
  31. <option value="3">Yen</option>
  32. </select>
  33. <b>To </b>
  34. <select id="to">
  35. <option value="0">USD</option>
  36. <option value="1">Euro</option>
  37. <option value="2">Pound</option>
  38. <option value="3">Yen</option>
  39. </select>
  40. <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);">
  41. <br>
  42. <div id="showcurrency"></div>
  43. </html>
  44.  

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
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Mar 3 '07

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
#7: Mar 4 '07

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
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Mar 5 '07

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...
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#9: Mar 5 '07

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
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#10: Mar 5 '07

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
#11: Mar 5 '07

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
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#12: Mar 6 '07

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
#13: Mar 8 '07

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

Expand|Select|Wrap|Line Numbers
  1. dollarAmount = amountEntered * currencyRates[sourceCurrency];
  2. amountConverted = dollarAmount / currencyRates[targetCurrency];
  3.  
  4. document.write('<BR>' + amountEntered + currencyNames[sourceCurrency] ' + ' currencyCodes[sourceCurrency] + ') ' + ' is ' + amountConverted + ' ' + 
  5. currencyNames[targetCurrency] + ' (' + currencyCodes[targetCurrency] + ') ');
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#14: Mar 8 '07

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:
Expand|Select|Wrap|Line Numbers
  1. document.write('<BR>' + amountEntered + ' ' + currencyNames[sourceCurrency] + ' (' + currencyCodes[sourceCurrency] + ') ' + ' is ' + amountConverted + ' ' + 
  2. currencyNames[targetCurrency] + ' (' + currencyCodes[targetCurrency] + ') ');
Newbie
 
Join Date: Mar 2007
Posts: 2
#15: Mar 16 '07

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...
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#16: Mar 16 '07

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
#17: Mar 16 '07

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