【问题标题】:The Currency Converter in JavascriptJavascript 中的货币转换器
【发布时间】:2017-10-16 22:22:11
【问题描述】:

我正在努力完成这个练习:

  1. 询问用户他们有多少钱。

  2. 接下来,问他们有什么货币

  3. 最后,问他们想把它转换成什么

  4. 将货币转换为请求的输出

  5. 打印一条漂亮的警报消息,其中包含其他货币的值

  6. 您的代码必须至少使用两个函数。

  7. 一个函数应该计算转换。

  8. 一个函数应该打印带有转换货币和货币名称的警报消息。

提示 - 使用两个转换函数可能是有意义的。一个函数将任何货币转换为美元,另一个函数将美元转换为任何其他货币。

这是我的代码:

当我在浏览器中运行代码时,什么也没有发生。 如果我正在计算正确的转换并且当我运行它时没有打印出来,我不会是真的。

更新: 好的,我解决了这个问题,现在它正在运行,但我仍然没有得到函数转换部分

'use strict';
let money = Number(prompt("How much money do you have?."));
let moneyCurrency = prompt("What currency are you using?.");
let currencysys = prompt("What currency do you want to convert it to? EUR, JPY, GBP, USD, or  BRL");


function convertCurrency(currency, currencySystem) {
  if (currencySystem === 'JPY' || currencySystem === 'jpy') {
    return convertJPYtoUSD(currency);
  } 
  else if (currencySystem  === 'Eur' || currencySystem  === 'eur') {
    return convertEurtoUSD(currency);
  }  
  else if (currencySystem  === 'GBP'|| currencySystem  === 'gbp') {
    return convertGBPtoUSD(currency);
  }
  else if ( currencySystem  === 'BRL'|| currencySystem  === 'brl') {
    return convertBRLtoUSD(currency);
  }
}

function convertJPYtoUSD(JPY) {
  return  (JPY * 0.91);
}

function printCurrencyMessage(convertedCurrency, currencysys,round) {
  if (round === undefined || isNaN(Number(round))) {
    round = 0;
  }
}

console.log("The converted currency is "+currencySystem + ".");

更新 2: 我有点麻烦,数学代码不正确 100GBP 到 JPY 应该是 19,103.08 但我得到了完全不同的东西

'use strict';
let money = Number(prompt("How much money do you have?."));
let moneyCurrency = prompt("What currency are you using?.");
let currencysys = prompt("What currency do you want to convert it to? EUR, JPY, GBP, USD, or  BRL");



`let currentExchange = {
"USD": 1,
"EURO" : 0.91,
"JPY" : 124.17,
"GBP" : 0.65,
"BRL" : 3.51,
}`

`let currentExchanges=currentExchange[currency];`

`function convertCurrency( currencyNeeded, moneyAmount) {
let exchange_value = moneyAmount * exchange_factor
return exchange_value


function convertCurrency( currencyNeeded, moneyAmount) {
let exchange_factor = currentExchange[currencyNeeded];
let exchange_value =  moneyAmount / exchange_factor
let value = moneyAmount * exchange_factor
console.log("The converted amount is $ " + (exchange_value.toFixed(2)) + "in " + currencyNeeded+ ".");

return exchange_value
};

convertCurrency(currencyNeeded,  moneyAmount);

【问题讨论】:

  • 请附上您的实际代码,而不仅仅是一张图片。同时描述您的问题。
  • 当我尝试添加我的代码时它不起作用,如果我正确计算转换并且当我运行它时没有打印出来,我不是真的
  • 如何运行脚本?为什么这里不能复制代码?
  • 我只是添加代码,我必须逐个添加代码
  • 代码对我有用:jsfiddle.net/ebmbpk71。您希望将代码打印在哪里?你检查过你的打印机吗?

标签: javascript function converter currency


【解决方案1】:

TDLR;

您正在尝试警告未定义的变量。

我建议你让你的函数为你返回你的值。比如:

console.log("The converted currency is "+ convertToCurrency(moneyCurrency, currencysys + ".");

解释和深入挖掘

你的签名

function convertCurrency(currency, currencySystem) {...

创建一个不能在函数范围之外引用的局部变量“currencySystem”。因此,currencySystem 是该函数的局部变量。

一些通用代码/架构建议:

使变量名更有意义和一致。

使用所有货币转换比率创建一个哈希图/对象,并使用单个函数进行数学转换。即:

var currencies = {
    usd: {
        conversion: 1,
    },
    eur: {
        conversion: .89
    },
    ...
};

function convertCurrency(fromCurrency, toCurrency, amount) {

    // ... not the best at thinking of the algorithm. Suggestions?
    // perhaps normalize fromCurrency to the dollar, then convert to toCurrency since I believe the dollar is the base/universal currency
};

var convertedCurrency = convertCurrency("USD", "EUR", 20.50);

alert("The converted amount is" + convertedCurrency);

如果您需要多个函数作为要求,拆分为每个货币转换不是一个可怕的想法,但似乎有点开销。

这是一个最终可行的解决方案:

'use strict';
let amount = Number(prompt("How much money do you have?."));
let currentCurrency = prompt("What currency are you using?");
let desiredCurrency = prompt("What currency do you want to convert it to? EUR, JPY, GBP, USD, or  BRL");

var currencyRates = {
    "USD": 1,
  "EUR": .8,
  "JPY": .7,
  "XXX": 5
};

function convertCurrency(currentCurrency, desiredCurrency, amount) {
  var currentRate = currencyRates[currentCurrency];
  var desiredRate = currencyRates[desiredCurrency];

  var USDAmount = amount * currentRate;
  var convertedAmount = USDAmount / desiredRate;

  return convertedAmount; // I think this is the right algorithm :/
}



var convertedCurrencyAmount = convertCurrency(currentCurrency, desiredCurrency, amount);

alert ("Converted: " + convertedCurrencyAmount);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多