The Currency Converter Helppppppp?

homework
javascript

#1

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.


#2

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}, 
  'CAD': {'USD': 1.2478, 'CAD': 80.14},
  '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!");
}

/*
CAD to USD = 1.2478
USD to CAD = 80.14
*/

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


#3

Yeah, I don’t understand


#4

Can you elaborate? What parts do you not understand?


#5

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


#6

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?


#8

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


#9

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


#10

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.


#12

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?


#13

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'}, 
    'CAD': {'USD': 1.2478, 'CAD': 0.8014, 'symbol': '$', 'currency': 'dollar'},
    '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 {
    let str = `${sell} (${data[sell]['currency']}) to ${buy} (${data[buy]['currency']}) = `, value;
    if (sell !== 'USD') {
      console.log(`${str}${value = data[sell][buy]}`);
    }
    else if (sell !== buy) {
      console.log(`${str}${value = data[buy][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`);
let exch_amnt = get_amnt('Buying');

try {
  console.log(`${data[trad_code]['symbol']}${convert(trad_code, exch_code) * exch_amnt} ${trad_code} \
to buy ${data[exch_code]['symbol']}${exch_amnt} ${exch_code}`);
  console.log(`${data[exch_code]['symbol']}${convert(exch_code, trad_code) * exch_amnt} ${exch_code} \
to buy ${data[trad_code]['symbol']}${exch_amnt} ${trad_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.


#15

update code:


#16

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


#17

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


#19

49 PM


#20

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

#21

Thank you it works now :slight_smile:


#22

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 55 PM


#23

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)

#24

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