/    Sign up×
Community /Pin to ProfileBookmark

new to javascript…please help with currency converter

Could anyone help with this please….With use of the following program my task is to now expand it in order for it to cope with four currencies (as shown below)

Dollar (USD) = 1 dollar
Euro (EUR) = 1.27 dollars
Poundsterling (GBP) = 1.85 dollars
japaneseYen (JPY) = 0.0085 dollars

var currencyType, amountEntered, amountConverted,euro,dollars;
currencyType = 0;
amountEntered = 0;
amountConverted = 0;
euro = 1;
dollar = 1.27;

currencyType = window.prompt(‘Please enter the currency 0) Dollar 1) Euro’, ‘ ‘);
currencyType = parseInt (currencyType);
while (currencyType < 0 currencyType >1)
{
currencyType = window.prompt(‘Please re-enter – currency should be 0 or 1’, ‘ ‘);
currencyType = parseInt (currencyType);
}
if (currencyType == 1)
{
currencyType = “dollars”;
}
else
if (currencyType ==1)
{
currencyType = “euro”;
}
amountEntered = window.prompt(‘Please enter the amount to be converted’, ‘ ‘);
amountEntered = parseFloat (amountEntered);

if (currencyType==”dollars”)
{
amountConverted = (amountEntered / dollars);
document.write(‘<BR>’ + amountEntered + ‘ of type ‘ + currencyType + ‘ is ‘ + amountConverted + )
{
else
if (currencyType==”euro”)
}
amountConverted = (amountEntered * dollars);
}

1.I need to prompt the user to first input a number to convert from(0 for dollars, 1 for euro, 2 for poundsterling and 3 for japaneseyen)

2.Prompt the user to input a number to convert to(0 for dollars, 1 for euro, 2 for poundsterling and 3 for japaneseyen)

3.Prompt the user to input the amount to be converted

4.Include the names of the currencies and their three-letter codes.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@netbuddyMar 17.2007 — Homework is for you to complete and not the forumboard members...
Copy linkTweet thisAlerts:
@suremadauthorMar 18.2007 — Quite rightly so, I am however on a tight deadline and cannot seem to get this running as it should. It's not for the want of trying, however I thought i'd ask advice from someone in this forum more knowledgeable than myself on this subject, I would then have some understanding of how the outcome is concluded. Thanks anyway!!
Copy linkTweet thisAlerts:
@Orc_ScorcherMar 18.2007 — Well, take a look at this. If you can't explain how it works your teacher isn't going to believe you wrote it anyway:/* const */ var currencies = [
{ name: "Euro", exchangeRate: 1.00, code: "EUR" },
{ name: "Yen", exchangeRate: 155.30, code: "JPY" },
{ name: "Pound sterling", exchangeRate: 0.6852, code: "GBP" },
{ name: "US Dollar", exchangeRate: 1.3303, code: "USD" }
]

function getNumber(message) {
do {
var input = prompt(message, "")
if (input == null)
throw "User cancelled."
} while (isNaN(input))
return +input
}

function getCurrency(direction) {
var message = "Please enter the currency to convert " + direction
var upper = currencies.length - 1
message += "(0 - " + upper +")n"
for (var i = 0; i &lt;= upper; ++i)
message += i + ") " + currencies[i].name + " "
var result
do {
result = Math.round(getNumber(message))
} while (!(result &gt;= 0 &amp;&amp; result &lt;= upper))
return result
}

function main() {
var from = getCurrency("from")
var to = getCurrency("to")
var amount = getNumber("Please enter the amount of "
+ currencies[from].name + " to be converted.")
var result = amount / currencies[from].exchangeRate * currencies[to].exchangeRate
document.write("&lt;p&gt;",
amount.toFixed(2), " ", currencies[from].name, " (",
currencies[from].code, ") are worth ", result.toFixed(2), " ",
currencies[to].name, " (" + currencies[to].code, ")."
)
}

try {
main()
}
catch (e) {
if (e != "User cancelled.") throw e
}
×

Success!

Help @suremad spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...