这是一个类(class)项目,我是一年级学生。我正在制作一个税务程序,一个函数计算该值所属的税级,下一个函数将格式化该值,连接美元符号,使用 .toFixed(2)
添加小数,并添加适当的逗号与 .toLocaleString()
。我只是想获取通过 formatMoney()
函数传递的值,以便我可以继续其余的编码。这个练习相当简单,但对于新手来说却很困难。任何帮助将不胜感激,我再次只希望该值通过第二个函数,以便我可以继续完成我可以做的其余编码,j
function calculateTax(money) {
let initAmount = parseFloat(prompt("How much do you have?"));
// The different taxes to be charged.
let tax1 = .20;
let tax2 = .15;
let tax3 = .05;
// Taking the user input and comparing it to which value if has to be calculated with.
if (initAmount <= 5) {
money = ((initAmount * tax1) + initAmount);
} else if (initAmount <= 100) {
money = ((initAmount * tax2) + initAmount);
} else if (initAmount >= 100.01) {
money = ((initAmount * tax3) + initAmount);
} else if (Number.isNaN(initAmount)) {
money = ("Invalid");
}
return (money);
}
calculateTax();
//This function will take the value of money from the calculateTax function and format as requested from client.
function formatMoney(money) {
let x = money;
console.log (x);
}
calculateTax(formatMoney(money));
最佳答案
我相信您正在寻找的代码如下。让我解释。此 console.log(calculateTax(100));
行通过调用 calculateTax
并传入 100
作为 initAmount
参数。然后代码运行您的 calculateTax
函数。当计算完金额后,它会将金额传递给 formatMoney
,这使得它很漂亮(我刚刚编写了一些代码,如果您想要良好的格式,请检查 here )。
//Example
console.log(calculateTax(100));
function calculateTax(initAmount) {
let tax1 = .20;
let tax2 = .15;
let tax3 = .05;
if (initAmount <= 5) {
money = ((initAmount * tax1) + initAmount);
} else if (initAmount <= 100) {
money = ((initAmount * tax2) + initAmount)
} else if (initAmount >= 100.01) {
money = ((initAmount * tax3) + initAmount);
} else if (Number.isNaN(initAmount)) {
money = ("Invalid");
}
let formattedMoney = formatMoney(money);
return (formattedMoney);
}
function formatMoney(money) {
let formattedMoney = '$' + money;
return formattedMoney;
}
关于javascript - 将税务程序中的一个函数传递给另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54485520/