好的,所以我必须为这个班的最后一个项目设计一个货币转换器。到目前为止,我已经获得了用户转换货币的if语句的if语句。但是问题是,我不确定如何为用户转换的第二种货币创建if语句,而不必在第一个选项的每个if语句中粘贴大量混乱的代码。
我已经尝试了一些创建具有我需要的功能的函数,但是我不确定是否可以或如何在原始函数中调用它。
此外,我打算用货币代码来在其下创建while循环,因此它会询问用户,然后根据代码的多少进行转换。为不同的货币保留了不同的金额。
function CurrencyConverter(){
var cad = "CAD";
var usd = "USD";
var gbp = "GBP";
var euro = "EUR";
var yen = "JPY";
var CurrencyCode = 0;
var inputted_currency = prompt("What currency is your amount in?");
if(inputted_currency == cad || inputted_currency == "Canadian" || inputted_currency == "Canadian Dollars"){
CurrencyCode + 1;
document.getElementById("FromCurrency").innerHTML = "CAD";
var twoC = call(SecondCurrency);
}
else if(inputted_currency == usd || inputted_currency == "American" || inputted_currency == "American Dollars"){
CurrencyCode + 2;
document.getElementById("FromCurrency").innerHTML = "USD";
var converted_currency = prompt("What currency would you like to convert to?");
}
else if(inputted_currency == gbp || inputted_currency == "British" || inputted_currency == "British Pounds" || inputted_currency == "Pounds"){
CurrencyCode + 3;
document.getElementById("FromCurrency").innerHTML = "GBP";
var converted_currency = prompt("What currency would you like to convert to?");
}
else if(inputted_currency == euro || inputted_currency == "European" || inputted_currency == "European Euros" || inputted_currency == "Euros"){
CurrencyCode + 4;
document.getElementById("FromCurrency").innerHTML = "EUR";
var converted_currency = prompt("What currency would you like to convert to?");
}
else if(inputted_currency == yen || inputted_currency == "Japanese" || inputted_currency == "Japanese Yen" || inputted_currency == "Yen"){
CurrencyCode + 5;
document.getElementById("FromCurrency").innerHTML = "JPY";
var converted_currency = prompt("What currency would you like to convert to?");
}
else if(inputted_currency == "Dollars"){
var dollar = prompt("Alright, Canadian or American?");
if (dollar == "American"){
CurrencyCode + 2;
document.getElementById("FromCurrency").innerHTML = "USD";
var converted_currency = prompt("What currency would you like to convert to?");
}
else if(dollar == "Canadian"){
CurrencyCode + 1;
document.getElementById("FromCurrency").innerHTML = "CAD";
var converted_currency = prompt("What currency would you like to convert to?");
}
else{alert("Sorry, that's not a valid option. Please enter either American or Canadian exactly")
}
}
else{alert("Sorry, that's not one of the supported currencies")
}
}
我希望的最终产品是,一旦用户单击一个按钮,他们就输入要转换的货币和该货币的金额。然后,转换器可以根据所投放的货币以及金额显示不同的金额。
如果我能从根本上简化代码,或者以某种方式将主代码链接到另一个代码块,我将不胜感激。谢谢您到目前为止所花的时间。
答案 0 :(得分:0)
我认为将一些信息放入两个对象中会大大受益。一个用于存储用户选择(国家,金额和目标国家),另一个用于将用户输入映射到国家信息。
var userInput = {
countryFrom: null,
amount: null,
countryTo: null
};
var countryMapping: {
'Canadian':{
CurrencyCode: 1,
countryCode: 'CAD',
},
'Canadian Dollars':{
CurrencyCode: 1,
countryCode: 'CAD',
},
//...repeat for all mappings
};
function currencyConverter(){
// first prompt
var inputted_currency = prompt("What currency is your amount in?");
if(countryMapping.hasOwnProperty(inputted_currency)){
var result = countryMapping[inputted_currency];
var countryCode = result.countryCode;
document.getElementById("FromCurrency").innerHTML = countryCode;
//var CurrencyCode = result.CurrencyCode;
userInput.countryFrom = countryCode;
// second prompt
var converted_currency = prompt("What currency would you like to convert to?");
if(countryMapping.hasOwnProperty(converted_currency)){
var secondResult = countryMapping[converted_currency];
var countryCode = secondResult.countryCode;
//var CurrencyCode = secondResult.CurrencyCode;
userInput.countryTo = secondResult.countryCode;
} else {
alert("Sorry, that's not one of the supported currencies")
}
} else {
alert("Sorry, that's not one of the supported currencies")
}
}
这不能处理从用户那里获取金额的问题。另外,我没有处理CountryCode,正如其他用户提到的那样,如果未将其分配给变量,则不会执行任何操作。