我正在开发一个项目,该项目将根据当前余额、利率和每月还款确定还清信用卡的时间和支付的利息。我已经能够让我的代码为我提供正确的余额、利息和最低还款额,一直到显示的最后几行。我在让我的表执行正确的输出时遇到了一些麻烦。我尝试过改变一些东西,但它似乎仍然显示不正确。我对 JavaScript 很陌生,所以我仍在努力学习基础知识,任何意见都将不胜感激。我将添加一个代码片段和一张我的表格应该是什么样子的图片。谢谢!
function displayWelcome() {
console.log("Welcome! \nThis program will determine the time to pay off a credit card and the interest paid based on the current balance, the interest rate, and the monthly payments made.")
}
function calculateminimumPaymentment(balance, minimumPaymentRate) {
return Math.max(20, balance * minimumPaymentRate);
}
function displayPayments(balance, interest, minimumPayment) {
console.log("Balance on your credit card: $" + balance.toFixed(2))
console.log("Interest Rate: " + (interest * 100) + "%")
console.log("Assuming a minimum payment of 2% of the balance ($20 min)")
console.log("Your minimum payment would be: $" + minimumPayment)
console.log("\nYear Balance Payment # Interest Paid Minimum Payment")
var year = 1;
var payments = 1;
var interestPaid = 0;
var yearChange;
while (balance > 0) {
yearChange = false;
if (payments % 12 == 0) {
year++
yearChange = true;
}
interestPaid += balance * interest / 12;
balance = Math.max(0, balance - (minimumPayment - balance * interest / 12));
minimumPayment = Math.max(20, balance * minimumPaymentRate);
console.log(yearChange ? year : "" + " " + balance.toFixed(2) + " " + payments + " " + interestPaid.toFixed(2) + " " + minimumPayment.toFixed(2));
payments++;
}
}
var balance = 1500;
var minimumPaymentRate = 0.02;
var interest = 0.18;
displayWelcome()
var minimumPayment = calculateminimumPaymentment(balance, minimumPaymentRate);
displayPayments(balance, interest, minimumPayment);
我在下面添加了两张图片,展示了我运行程序时所看到的内容以及程序在下面的两个链接中应该是什么样子。
This is the output I am receiving
This is how the table is supposed to look
问题在于运算符优先级。这?:
条件运算符的优先级低于+
, 所以:
部分包含所有连接的字符串,而不仅仅是""
应替换年份的字符串。所以你显示年份yearChange
是真的,只有当你显示其他所有内容时yearChange
是假的。
解决方案是将条件表达式括在括号中。
function displayWelcome() {
console.log("Welcome! \nThis program will determine the time to pay off a credit card and the interest paid based on the current balance, the interest rate, and the monthly payments made.")
}
function calculateminimumPaymentment(balance, minimumPaymentRate) {
return Math.max(20, balance * minimumPaymentRate);
}
function displayPayments(balance, interest, minimumPayment) {
console.log("Balance on your credit card: $" + balance.toFixed(2))
console.log("Interest Rate: " + (interest * 100) + "%")
console.log("Assuming a minimum payment of 2% of the balance ($20 min)")
console.log("Your minimum payment would be: $" + minimumPayment)
console.log("\nYear Balance Payment # Interest Paid Minimum Payment")
var year = 1;
var payments = 1;
var interestPaid = 0;
var yearChange;
while (balance > 0) {
yearChange = false;
if (payments % 12 == 0) {
year++
yearChange = true;
}
interestPaid += balance * interest / 12;
balance = Math.max(0, balance - (minimumPayment - balance * interest / 12));
minimumPayment = Math.max(20, balance * minimumPaymentRate);
console.log((yearChange ? year : "") + " " + balance.toFixed(2) + " " + payments + " " + interestPaid.toFixed(2) + " " + minimumPayment.toFixed(2));
payments++;
}
}
var balance = 1500;
var minimumPaymentRate = 0.02;
var interest = 0.18;
displayWelcome()
var minimumPayment = calculateminimumPaymentment(balance, minimumPaymentRate);
displayPayments(balance, interest, minimumPayment);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)