表打印输出错误JavaScript

时间:2018-04-11 23:03:11

标签: javascript

我正在开展一个项目,该项目将确定偿还信用卡的时间以及根据当前余额,利率和每月付款支付的利息。我已经能够让我的代码给我正确的余额,利息和最低付款,一直到显示的最后几行。我一直在让我的表执行正确的输出时遇到一些麻烦。我试过改变一些东西,但它似乎仍然显示不正确。我是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);

我在运行程序时添加了两张下面的图片,以及下面两个链接中的程序应该是什么样子。

这是我收到的输出

这就是表格的外观

1 个答案:

答案 0 :(得分:0)

问题是运营商优先级。 ?:条件运算符的优先级低于+,因此:部分包含所有连接的字符串,而不仅仅是应该替换年份的""字符串。因此,当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);