我正在尝试完成此练习:
询问用户他们有多少钱。
接下来,询问他们有哪种货币
最后,问问他们想要将其转换为
将货币转换为请求的输出
使用其他货币的值
您的代码必须至少使用两个函数。
一个函数应该计算转换。
一个函数应该使用转换后的货币和货币名称打印警报消息。
提示 - 使用两个转换函数可能是有意义的。一种功能是从任何货币转换为美元,另一种功能从美元转换为任何其他货币。
这是我的代码:
当我在浏览器中运行代码时,没有任何反应。 如果我正在计算转换权,那么我不是真的,当我运行时,没有任何打印输出。
更新: 好吧,我解决了问题所以它现在正在运行,但我仍然没有得到转换部分的功能
'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);
答案 0 :(得分:0)
<强> TDLR; 强>
您正在尝试警告未定义的变量。
我建议你让你的功能为你回报你的价值。类似的东西:
console.log("The converted currency is "+ convertToCurrency(moneyCurrency, currencysys + ".");
更深入地解释和挖掘
您的签名
function convertCurrency(currency, currencySystem) {...
创建一个局部变量“currencySystem”,它不能在函数范围之外引用。因此,currencySystem是该函数的局部变量。
一些通用代码/架构建议:
使变量名更有意义和一致。
使用所有货币转换比率创建一个hashmap / object,并使用单个函数进行数学转换。 IE:
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);