473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Creating a currency converter using three parrallel arrays

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
16 9459
iam_clint
1,208 Expert 1GB
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
1,208 Expert 1GB
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
mrhoo
428 256MB
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
lors
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
16,027 Expert Mod 8TB
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
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
16,027 Expert Mod 8TB
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
16,027 Expert Mod 8TB
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
1,208 Expert 1GB
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
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
16,027 Expert Mod 8TB
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
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
16,027 Expert Mod 8TB
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
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
1,208 Expert 1GB
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
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

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

Similar topics

2
by: techy techno | last post by:
hii Experts..!! I need someone to tell me where I can get a Currency Converter like http://cconv.textor.com please can someone tell me where I can get it I need it for free + I dont need...
7
by: meenasamy | last post by:
Hi all, i need to create a function that takes three parameters( Original currency, needed currency, amount) i need to convert from the original currency to the needed currency the amount and...
2
by: Rash33d | last post by:
Hi there! could som1 please help me out? i need toknow if there's something wrong with this javascript code. The code is meant to give the american dollar and european equivalent of a price. i.e...
10
by: jayender.vs | last post by:
Hello guys, I need to know the Currency conversion code in Javascript Say for example i got 2 textbox .. where i enter a number (singapore doller value) and i provide a button and in the next text...
2
by: Raj | last post by:
Is there a current converter web service available? Currency rates are to be as per latest exchange rates. Raj.
1
by: Nor farhana yaakub | last post by:
hye,i try to implement this program,which is to keep track of no ofstudents together with their marks.i suppose that maximum no of students is 40.and i stored all the input in farhanafiona.txt and i...
25
by: mereba | last post by:
Hello My country Ghana is changing its currency. I want to write a small programme in C++ that can covert from the old currency into the new one. I would like this programme to run behind a simple...
7
by: tararreb | last post by:
#include<stdio.h> /*This line is standard input output, # is directive, include is keyword, and stdio.h is header file*/ #include<stdlib.h> /*This line is standard input output, # is directive,...
1
by: katie20 | last post by:
Hi everyone, I have written a code in order to produce a currency converter. My program will be designed to convert pounds into euros using visual basic programming. I have two text boxes and one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.