表格输出 JavaScript 打印不正确

2023-12-03

我正在开发一个项目,该项目将根据当前余额、利率和每月还款确定还清信用卡的时间和支付的利息。我已经能够让我的代码为我提供正确的余额、利息和最低还款额,一直到显示的最后几行。我在让我的表执行正确的输出时遇到了一些麻烦。我尝试过改变一些东西,但它似乎仍然显示不正确。我对 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(使用前将#替换为@)

表格输出 JavaScript 打印不正确 的相关文章

随机推荐

  • Java EE 6 与 Spring 3 堆栈 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心以获得指导 我现在正在开始一个新项目
  • 如何在 Angular 中重现 Angular JS .broadcast() / .on() 行为?

    我正在努力用 Angular8 重写 AngularJS 应用程序 我已经阅读了组件之间通信的不同方法 但鉴于我当前的要求 似乎无法找到实现此目的的正确方法 我目前有 2 个兄弟组件 它们都使用处理基本 CRUD 功能的通用服务 表单组件调
  • 将 ORB 特征与阈值进行匹配

    我的项目是基于android的草药识别 我使用 ORB 来获取关键点 特征并匹配特征 我想使用这个算法 我使用 4 个参考图像 并将它们的特征 image1 与 image1 1 2 1 3 1 4 2 3 3 4 进行匹配 然后我将到数据
  • SQL Server Express 4GB 限制

    简单问题 4GB 限制是针对每个数据库还是针对已安装的 SQL Server 实例 如您所知 您可以在 SQL Server 实例中创建多个数据库 每个数据库的大小限制为 4GB
  • 控制范围和预测范围

    我已经回顾了模型预测控制的参考书目和 Gekko 编程结构 尽管我了解它的编程方式及其目的 例如 我想了解 Gekko 如何根据 Seborg 中的相关内容来管理控制范围和预测范围之间的差异 我看不出代码有什么区别 下面是一个用于说明的 M
  • Socket_read() 说“不是有效的资源”

    我正在学习套接字编程并尝试使用 php 我想使用客户端连接到套接字服务器并从客户端读取服务器的响应 代码 服务器 php address 127 0 0 1 port 3343 echo I am here set time limit 0
  • 将枚举列表传递给条件

    我有域名 付款 class Payment String name PaymentType paymentType PaymentType 是一个 ENUM 搜索特定付款类型的所有付款很简单 def results Payment crea
  • 单成员结构的灵气属性传播问题

    我遇到了 Spirit Qi 的编译问题 它抱怨说值类型不是以下成员标识符 由于某种原因 Qi 的属性系统将标识符视为容器类型 并尝试枚举它的值类型 这是一个类似的问题这个问题 但是 我相信原因是单个成员结构和may与此有关bug incl
  • 如何为应用程序的容器使用相同的凭证处理程序配置来生成新的密码哈希值?

    我的 Web 应用程序的上下文定义类似于
  • php 文本中最常用的单词

    我在 stackoverflow 上找到了下面的代码 它可以很好地查找字符串中最常见的单词 但我可以排除对 a if you have 等 等常用词的计数吗 或者我必须在计数后删除元素吗 我该怎么做 提前致谢
  • Android BLE 血糖通知

    我厌倦了使用 Android BLE SDK 与我的 Glucose 设备进行通信 我需要 UUID 2a18 和 2a34 的 setCharacteristicNotification 我参考Android官方SDK如下 http de
  • 错误:[Dagger/MissingBinding] Map、Provider>

    我有 Dagger MissingBinding 的问题 我在 stackoverflow 上红色了所有相关答案 我尝试使用不同版本的 kotlin gradle dagger 我尝试使用不同的解决方法 例如kapt correctErro
  • Crystal Reports:数据库登录失败

    我在 ASP NET C 中有一个应用程序 其中使用 Sap Crystal Reports for Visual Studio 在 VS 中一切都很完美 但是当我将其部署到 IIS 时 出现 数据库登录失败 错误 当我设置 Crystal
  • Access 中的 SELECT @@IDENTITY 始终返回 0

    我一直在努力寻找解决这个问题的方法 但到目前为止没有任何效果 private void Insert string ConnectionStringAccess Provider Microsoft ACE OLEDB 12 0 Data
  • 跨页面发布

    我只是尝试跨页面发布的示例 我已添加 1 个文本框和 1 个按钮到 default aspx 页面
  • 强制下线所有在线用户中的特定用户

    在我的网站管理员可以查看所有其他在线用户的列表 管理员还可以禁用该列表中的任何帐户 到目前为止一切都很顺利 但现在我决定注销被禁用的用户 如何对上述指定在线用户列表中的特定用户进行注销操作 注意 我对 SQL Server 数据库使用默认成
  • jQuery:获取表中的所有输入值并序列化

    给定以下表结构 当 button被点击了 table tr td td tr table
  • GMail fsockopen():Codeigniter 和 XAMPP 的 SSL 操作失败错误

    错误消息 1 消息 fsockopen SSL 操作失败 代码为 1 OpenSSL 错误消息 错误 14090086 SSL 例程 ssl3 get server certificate 证书验证失败 文件名 库 Email php 线路
  • 如何阻止 r devtools::check() 注意到 .DS_Store

    每次我跑步devtools check 在我的包裹上有一条注释 gt checking for non standard things in the check directory NOTE Found the following file
  • 表格输出 JavaScript 打印不正确

    我正在开发一个项目 该项目将根据当前余额 利率和每月还款确定还清信用卡的时间和支付的利息 我已经能够让我的代码为我提供正确的余额 利息和最低还款额 一直到显示的最后几行 我在让我的表执行正确的输出时遇到了一些麻烦 我尝试过改变一些东西 但它