我正在努力完成这个练习:
询问用户有多少钱。
接下来,询问他们有什么货币
最后,询问他们想要将其转换成什么
将货币转换为所需的输出
打印一条带有其他货币价值的漂亮警报消息
您的代码必须至少使用两个函数。
一个函数应该计算转换。
一个函数应打印带有转换后的货币和货币名称的警报消息。
提示 - 使用两个转换函数可能是有意义的。一 函数可将任何货币转换为美元,并可将另一种货币转换为美元 从美元兑换成任何其他货币。
这是我的代码:
当我在浏览器中运行代码时,没有任何反应。 如果我正确计算转换并且当我运行此命令时什么也没有打印出来,我就不是真的。
我解决了问题,所以它现在正在运行,但我仍然没有得到函数转换部分
'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 + ".");
我遇到了一些麻烦,数学代码不正确 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);
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);