# The Currency Converter Helppppppp?

The two errors I’m getting are :
Uncaught ReferenceError: convertUSDtoUSD is not defined

and
Calculating the currency is wrong

Here is my code:

//1. At the start of your program,
//2. Ask the user how much money they have.
//3. Next ask them what currency they have
//4. finally ask them what they want to convert it into
//5. Convert the currency to the requested output
//6. Print a nice alert message with the value in the other currency
//Your code must use a minimum of two functions.
//One function should calculate the conversion.
//One function should print the alert message with the converted currency and the currency name.
//Hint - It probably makes sense to use two conversion functions. One function to convert from any currency into USD and another to convert from USD into anything other currency.

To force string output (in case the user clicks Cancel or presses the Esc key) you can coerce the output to a String object, just as you have coerced the Number input.

Consider also that the options are the same for both string inputs.

https://en.wikipedia.org/wiki/ISO_4217

https://www.oanda.com/currency/live-exchange-rates/

https://www.bloomberg.com/markets/currencies

``````const codes = ['JPY', ' EUR', ' GBP', ' BRL', ' CAD', ' USD'];

const usd_conv_factors = {
'JPY': {'USD': 111.83, 'JPY': 0.0088},
'EUR': {'USD': 0.8453, 'EUR': 1.1812},
'GBP': {'USD': 1.3293, 'GBP': 75.19},
'BRL': {'USD': 3.1460, 'BRL': 0.3179},
'USD': {'USD': 1.0000}
};
``````
``````let code_out, code_in;

try {
code_out = String(prompt(`Trading currency code?\n\${codes.toString()}`, `USD`)).toUpperCase();
code_in = String(prompt(`Exchange currency code?\n\${codes.toString()}`, `USD`)).toUpperCase();

// to USD works directly
if (code_out !== 'USD') {
console.log(`\${code_out} to \${code_in} = \${usd_conv_factors[code_out][code_in]}`);
}
// from USD works indirectly
else if (code_in !== code_out) {
console.log(`\${code_out} to \${code_in} = \${usd_conv_factors[code_in][code_in]}`);
}
}
catch (e) {
console.log("Error!");
}

/*
*/
``````

The above should by no means be used in your solution as that would be copying, but it demonstrates how we can separate the data from the processing. These returned values need only be multiplied by the amount being traded.

https://repl.it/Mf46

1 Like

Yeah, I don’t understand

Can you elaborate? What parts do you not understand?

1 Like

Sorry, but I’m new to javascript, so all of it

Do you have a classroom advisor? Can you go to your teacher for help, and maybe a little brushing up?

This is a pretty complex problem for a greenhorn beginner. Can you update us on what you have covered so far?

1 Like

This is for a class project and I’m just not getting it

Why are you putting all the exchange currency in a object ? I mean the converted currency

It puts all the necessary data in one place which is easy to maintain and update. If the data is embedded in the code then that will be nightmare to keep updated. Notice there is no math. Just data retrieval? The math portion will be to multiply by the amount of money to exchange.

1 Like

This is the Assignment

You are to create a program that can convert money from one system to another.

At the start of your program, ask the user how much money they have. Next ask them what currency they have and finally ask them what they want to convert it into. Convert the currency to the requested output and print a nice alert message with the value in the other currency.

This is what I has so far
Does this look right?

Not really, no. But your classmates should be in on this so the code is uniform and not bloated.

``````const codes = [' JPY', ' EUR', ' GBP', ' BRL', ' CAD', ' USD'];

const data = {
'JPY': {'USD': 111.83, 'JPY': 0.0088, 'symbol': '¥', 'currency': 'yen'},
'EUR': {'USD': 0.8453, 'EUR': 1.1812, 'symbol': '€', 'currency': 'euro'},
'GBP': {'USD': 1.3293, 'GBP': 0.7519, 'symbol': '£', 'currency': 'pound'},
'BRL': {'USD': 3.1460, 'BRL': 0.3179, 'symbol': 'R\$', 'currency': 'real'},
'USD': {'USD': 1.0000, 'usd': 1.0000, 'symbol': '\$', 'currency': 'dollar'}
};
``````
``````const get_code = noun => String(prompt(`\${noun} currency code?\n\${codes.toString()}`,`USD`)).toUpperCase();

const get_amnt = verb => Number(prompt(`Enter the amount you are \${verb}`));

const convert = (sell, buy) => {
try {
if (sell !== 'USD') {
}
else if (sell !== buy) {
} else {
console.log(`\${str}1.0000`);
value = 1;
}
return value;
}
catch (e) {
console.log("Error!");
}
};
``````
``````let trad_code = get_code(`Trading`);
let exch_code = get_code(`Exchange`);

try {
console.log(`\${data[exch_code]['symbol']}\${convert(exch_code, trad_code) * exch_amnt} \${exch_code} \
}
catch (e) {
console.log("Error!");
}
``````

By storing the data in an object, our functions can call upon them for values. We should never write constant values in functions, if at all avoidable.

2 Likes

update code:

I thought your teacher said you could not use arrays or objects? Did he have a change of heart?

1 Like

Yup Thank god too because I couldn’t figure it out with one. SO when I run this code the answer always print out NaN

The conversion factor needs to be retrieved from the currentRate object. It will matter which way the conversion is taking place. To USD or From USD.

To convert from JPY to USD,

``````let conversion_factor = currentRate[currentCurrency]['conversion']

let converted_value = moneyAmount * conversion_factor
``````

To convert from USD to JPY,

``````let conversion_factor = currentRate[currencyNeeded]['conversion']

let converted_value = moneyAmount / conversion_factor
``````
1 Like

Thank you it works now

1 Like

So im trying to make sure that the math is right, when i add the currency 100 GBP TO JPY it should be 191,03.08 but im not getting that answer

I meant to mention this earlier… You have two functions with the same name.

``````19,103.08  =>  19103.08
``````

That number is correct if you are getting it like this…

``USD / JPY * GBP * 100  (in any order)``
1 Like

oh okay, I will change that now also my console log isn’t working