I am writing a program for my class and we have not really gotten into how to format numbers with a comma, I have done some research and tried to use the toLocalestring(), but was not able to get it to work. I am not sure if I didn't put it in the right spot or if there is another way to put the comma where I want it. I will post my code to see if anyone has an idea of what I need to do. I only need the commas to show up in the balance and interest paid columns. Also I do have to use console.log() as per class requirement at this point.
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 - 10), 'Payment Number'.padStart(pad - 1), 'Interest Paid'.padStart(pad - 4), 'Minimum Payment'.padStart(pad - 3));
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 - 9),
('' + paynum).padStart(pad - 3),
'$' + parseFloat(interestPaid).toFixed(2).padStart(pad - 5),
'$' + parseFloat(minimumPayment).toFixed(2).padStart(pad - 3)
);
} else {
console.log(
''.padStart(pad),
'$' + parseFloat(balance).toFixed(2).padStart(pad - 9),
('' + paynum).padStart(pad - 3),
'$' + parseFloat(interestPaid).toFixed(2).padStart(pad - 5),
'$' + parseFloat(minimumPayment).toFixed(2).padStart(pad - 3)
);
}
yearcount++;
}
}
var balance = 1500;
var minimumPaymentRate = 0.02;
var interest = 0.18;
displayWelcome();
var minimumPayment = calculateminimumPaymentment(balance, minimumPaymentRate);
displayPayments(balance, interest, minimumPayment);