昨天我问了一个关于我桌子格式的类似问题。我最终改变了很多代码,试图让它正确输出,但仍然有一些麻烦。当它以单位数字输出年份时,我能够使它成为正确的格式,之后格式将不会调整到我设置的格式。我也试图在天平上的数字中添加逗号,但没有运气。这是我第一次参加JavaScript课程,所以我很无能,本书中的信息不是很有帮助。此外,我必须使用console.log()作为我的输出,因为我们还没有教过任何其他输出。我会留下我的代码和一些照片。任何想法都会很棒,因为我几乎走到了尽头。
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 Number Interest Paid Minimum Payment")
var acum = 0;
var yearcount = 0;
var paynum = 0;
var interestPaid = 1;
var year = 0;
while (balance > 0) {
paynum++;
interestPaid = balance * (interest / 12);
balance = Math.max(0, balance - (minimumPayment - balance * interest / 12));
minimumPayment = Math.max(20, balance * minimumPaymentRate);
acum = (parseFloat(acum) + parseFloat(interestPaid)).toFixed(2);
var tbl1 = "$" + parseFloat(balance).toFixed(2) + "\t\ " + paynum + " " + "$" + "\t" + parseFloat(interestPaid).toFixed(2) + " " + "$" + "\t\t" + parseFloat(minimumPayment).toFixed(2);
if (yearcount % 12 === 0) {
year++
var tbl2 = " " + year + " " + tbl1;
} else {
var tbl2 = "\t" + tbl1;
}
yearcount++;
console.log(tbl2);
}
}
var balance = 1500;
var minimumPaymentRate = 0.02;
var interest = 0.18;
displayWelcome()
var minimumPayment = calculateminimumPaymentment(balance, minimumPaymentRate);
displayPayments(balance, interest, minimumPayment);
This is the correct format that is being displayed
答案 0 :(得分:0)
使用Xotic750的评论。忽略代码段的控制台输出,并在浏览器的控制台中查看。
function displayWelcome() {
console.log("Welcome!");
console.log("This 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) {
var pad = 17;
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();
console.log('Year'.padStart(pad), 'Balance'.padStart(pad), 'Payment Number'.padStart(pad), 'Interest Paid'.padStart(pad), 'Minimum Payment'.padStart(pad));
var acum = 0;
var yearcount = 0;
var paynum = 0;
var interestPaid = 1;
var year = 0;
while (balance > 0) {
paynum++;
interestPaid = balance * (interest / 12);
balance = Math.max(0, balance - (minimumPayment - balance * interest / 12));
minimumPayment = Math.max(20, balance * minimumPaymentRate);
acum = (parseFloat(acum) + parseFloat(interestPaid)).toFixed(2);
if (yearcount % 12 === 0) {
year++;
console.log(
('' + year).padStart(pad),
'$' + parseFloat(balance).toFixed(2).padStart(pad - 1),
('' + paynum).padStart(pad),
'$' + parseFloat(interestPaid).toFixed(2).padStart(pad - 1),
'$' + parseFloat(minimumPayment).toFixed(2).padStart(pad - 1)
);
} else {
console.log(
''.padStart(pad),
'$' + parseFloat(balance).toFixed(2).padStart(pad - 1),
('' + paynum).padStart(pad),
'$' + parseFloat(interestPaid).toFixed(2).padStart(pad - 1),
'$' + parseFloat(minimumPayment).toFixed(2).padStart(pad - 1)
);
}
yearcount++;
}
}
var balance = 1500;
var minimumPaymentRate = 0.02;
var interest = 0.18;
displayWelcome();
var minimumPayment = calculateminimumPaymentment(balance, minimumPaymentRate);
displayPayments(balance, interest, minimumPayment);