By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,286 Members | 2,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,286 IT Pros & Developers. It's quick & easy.

Creating a currency converter using three parrallel arrays

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

Expand|Select|Wrap|Line Numbers
  1. var currency;
  2. var amountEntered;
  3. currency = window.prompt('Please enter 0 to convert from Dollars to euros and 1 to convert form euros to dollars',''); 
  4. currency = parseFloat(currency);
  5. while (currency < 0 || currency > 1)  
  6. {
  7. currency = window.prompt('Please re enter currency should be 0 or 1','');
  8. currency = parseFloat(currency);
  9. }
  10. amountEntered = window.prompt('Please enter the amount to be converted',''); 
  11. amountEntered = parseFloat(amountEntered);
  12. if (currency == 0 )
  13. {
  14. document.write('<BR>' + amountEntered + ' Dollars converts to ' + amountEntered / 1.27 + ' Euros')
  15. currency = parseFloat (currency);
  16. }
  17. else
  18. {
  19. document.write('<BR>' + amountEntered + ' Euros converts to ' + amountEntered * 1.27 + ' Dollars')
  20. }
  21.  
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
Feb 15 '07 #1
Share this Question
Share on Google+
16 Replies


iam_clint
Expert 100+
P: 1,208
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
Feb 15 '07 #2

iam_clint
Expert 100+
P: 1,208
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
Feb 15 '07 #3

100+
P: 428
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).
Feb 16 '07 #4

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

acoder
Expert Mod 15k+
P: 16,027
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.
Mar 3 '07 #6

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

acoder
Expert Mod 15k+
P: 16,027
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...
Mar 5 '07 #8

acoder
Expert Mod 15k+
P: 16,027
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
Mar 5 '07 #9

iam_clint
Expert 100+
P: 1,208
Parrallel Arrays!!!!

var p = new Array();
p[0] = "USD";
p[1] = "YEN";
var b = new Array();
b[0] = 0;
b[1] = 1;


oh no.
Mar 5 '07 #10

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

acoder
Expert Mod 15k+
P: 16,027
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.
Mar 6 '07 #12

P: 3
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] + ') ');
Mar 8 '07 #13

acoder
Expert Mod 15k+
P: 16,027
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] + ') ');
Mar 8 '07 #14

P: 2
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...
Mar 16 '07 #15

iam_clint
Expert 100+
P: 1,208
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
Mar 16 '07 #16

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

Post your reply

Sign in to post your reply or Sign up for a free account.