我如何每 12 次递增一次并使用对象文字和 while 循环保持 运行 总数?
How do i Increment every 12th time and Keeping a running total with an object literal and while loop?
一点历史。
这是我上交的家庭作业问题。他们希望我在 12 个月付款后增加这段代码中的年份,并且记录我支付的利息总额 运行。代码有效或不引发错误。
问题。
我不知道如何每 12 次迭代递增对象文字 属性。 (为了表明您已经支付了十二笔款项并且下一年开始了)我想不出如何保持 运行 支付的利息总额。
我尝试过的东西。
我在 属性 年尝试过使用循环。要么无法完成,要么我做错了什么。我尝试使用 if else 根据付款 ID 增加年份,但它只增加一次。我尝试在循环前后增加年份。然后同步支付ID,部分成功?
function displayWelcome() {
printLine();
console.log('Welcome to the credit card payoff tool.');
printLine();
console.log('This program will determine the time it \nwill take to pay off a credit card!');
printLine();
}
displayWelcome();
function calculateMinimumPayment(balance, interestRate) {
var calcMinPay = balance * interestRate;
console.log(calcMinPay);
return calcMinPay
}
function generatePaymentId() { // lines 14 - 22 are a closure function.
var count = 0;
function paymentId() {
count ++;
return count;
}
return paymentId;
};
var id = generatePaymentId();
function processPaymentSchedule(balance, interest, minimumPayment) {
var year = 1;
var counter = 0;
while (balance > 0) {
counter ++;
year += 0.08333333333;
var interestDecimal = interest / 100;
var interestMonthly = interestDecimal / 12;
var interestPaid = balance * interestMonthly;
var princplePaid = minimumPayment - interestPaid;
var balance = balance - princplePaid;
var payment = {
year: year.toFixed(0),
balance: balance.toFixed(2),
paymentId: id(),
interestPaid: interestPaid.toFixed(2)
};
function definePayment() {
console.log(payment);
}
definePayment();
if (payment.paymentId > 11) {
year += 1;
}
}
}
function printLine() {
console.log('---------------------------------------');
}
processPaymentSchedule (1500, 18, 30);
错误:这段代码中没有错误信息,只是不知道如何做上面列出的事情。
要确定它是否是 "every Nth" 迭代计数,您可以使用模运算符 %
从基本整数除法中获取余数。只需将您的计数器变量乘以 12 取模,如果计数器大于 0,则它位于 12 日、24 日等。运行 通过您的循环。
var counter = 0;
while (balance > 0) {
counter ++;
if (counter%12==0)
console.log("You are on a (N*12)th loop run");
}
}
但是,在计算利息和付款等方面,我建议您在 "month" 中进行所有计算,然后当您得到以月为单位的总数时,如果 needed/desired.
一点历史。
这是我上交的家庭作业问题。他们希望我在 12 个月付款后增加这段代码中的年份,并且记录我支付的利息总额 运行。代码有效或不引发错误。
问题。
我不知道如何每 12 次迭代递增对象文字 属性。 (为了表明您已经支付了十二笔款项并且下一年开始了)我想不出如何保持 运行 支付的利息总额。
我尝试过的东西。
我在 属性 年尝试过使用循环。要么无法完成,要么我做错了什么。我尝试使用 if else 根据付款 ID 增加年份,但它只增加一次。我尝试在循环前后增加年份。然后同步支付ID,部分成功?
function displayWelcome() {
printLine();
console.log('Welcome to the credit card payoff tool.');
printLine();
console.log('This program will determine the time it \nwill take to pay off a credit card!');
printLine();
}
displayWelcome();
function calculateMinimumPayment(balance, interestRate) {
var calcMinPay = balance * interestRate;
console.log(calcMinPay);
return calcMinPay
}
function generatePaymentId() { // lines 14 - 22 are a closure function.
var count = 0;
function paymentId() {
count ++;
return count;
}
return paymentId;
};
var id = generatePaymentId();
function processPaymentSchedule(balance, interest, minimumPayment) {
var year = 1;
var counter = 0;
while (balance > 0) {
counter ++;
year += 0.08333333333;
var interestDecimal = interest / 100;
var interestMonthly = interestDecimal / 12;
var interestPaid = balance * interestMonthly;
var princplePaid = minimumPayment - interestPaid;
var balance = balance - princplePaid;
var payment = {
year: year.toFixed(0),
balance: balance.toFixed(2),
paymentId: id(),
interestPaid: interestPaid.toFixed(2)
};
function definePayment() {
console.log(payment);
}
definePayment();
if (payment.paymentId > 11) {
year += 1;
}
}
}
function printLine() {
console.log('---------------------------------------');
}
processPaymentSchedule (1500, 18, 30);
错误:这段代码中没有错误信息,只是不知道如何做上面列出的事情。
要确定它是否是 "every Nth" 迭代计数,您可以使用模运算符 %
从基本整数除法中获取余数。只需将您的计数器变量乘以 12 取模,如果计数器大于 0,则它位于 12 日、24 日等。运行 通过您的循环。
var counter = 0;
while (balance > 0) {
counter ++;
if (counter%12==0)
console.log("You are on a (N*12)th loop run");
}
}
但是,在计算利息和付款等方面,我建议您在 "month" 中进行所有计算,然后当您得到以月为单位的总数时,如果 needed/desired.