在此 PF 计算函数中循环分配动态变量

Assign dynamic variable in loop in this PF calculation function

提问人:Selvakumar 提问时间:8/25/2023 更新时间:8/25/2023 访问量:28

问:

嗨,我正在尝试创建一个 SPF 计算器,在这里我需要计算加入日期和退休数据之间几个月的利息,我将这些指标之间的时间段转换为月份。我在循环函数中静态分配条件以计算不同年份的特定利息。如果我从这个意义上分配不同的连接数据和保留数据,则利息值将显示 - NaN。所以它必须在循环函数月条件中动态计算变量。
如果从 2001-01-01 到 2021-12-31,它应该是这样的

如果从 2001-01-01 到 2021-12-31,它应该是这样的

将数据 2005-01-01 更改为 2025-12-31 时,显示错误将数据 2005-01-01 更改为 2025-12-31 时,显示错误:

在下面的循环函数条件中,我必须给出月份作为动态值:**

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Special Provident Fund Calculator</title>
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <style>
    input { width:auto; }
    table, td, th { border: 1px solid #525050; }
    td, th { padding: 5px; column-gap: 0cap; }
  </style>
</head>
<body>
  <div class="calculator">
    <!-- <h1>Special Provident Fund Calculator 2000</h1> -->
    <p>
      <label for="contributionPeriod">Total Months Worked :</label>
      <span id="contributionPeriod"></span>
    </p>
    <p>
      <label for="subscriptionEndDate">Subscription Completed Date:</label>
      <span id="subscriptionEndDate"></span>
    </p>
    <p>
        <span>Total Interest Amount: <span id="totalInterest"></span>
    </p>
    <table>
      <tr>
        <td>
          <label for="joiningDate">Joining Date:</label>
          <input type="text" id="joiningDate" class="datePicker" required>
        </td>
        <td>
          <label for="retainmentDate">Retainment Date:</label>
          <input type="text" id="retainmentDate" class="datePicker" required>
        </td>
        <td>
          <div id="subscriptionAmount">
            <label for="monthlySubscriptionAmount">Monthly Subscription Amount:</label>
            <select id="monthlySubscriptionAmount">
              <option value="50">50</option>
              <option value="70">70</option>
            </select>
          </div>
        </td>
        <td>
          <button id="calculate">Calculate</button>
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <label for="subscriptionMonths">Subscription Months</label>
          <span id="subscriptionMonths">147</span> <span id="subVal"></span> = <span id="result"></span>
        </td>
        <td>
          <p>Interest Amount: <span id="interest"></span></p>
        </td>
        <td>
          <p>Current Balance: <span id="CurrentBalance"></span></p>
        </td>
      </tr>
    </table>
  </div>
  
  <h2>Installment Details</h2>
  <table id="installmentTable">
    <tr>
      <th>Instalment</th>
      <th>Month name - year</th>
      <th>Opening Balance</th>
      <th>Subscription</th>
      <th>Total Amount</th>
      <th>Interest Rate</th>
      <th>Interest Amount</th>
      <th>Current Balance</th>
    </tr>
  </table>
  <table>
  <p style=""><span>Total Interest Amount: <span id="intttl"></span></p>

  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
     $(document).ready(function() {
    $(".datePicker").datepicker({
      dateFormat: "yy-mm-dd",
    });

    $("#calculate").on("click", function() {
      const joiningDate = new Date($("#joiningDate").val());
      const retainmentDate = new Date($("#retainmentDate").val());
      const subscriptionMonths = parseInt($("#subscriptionMonths").text());
      const monthlySubscriptionAmount = parseFloat($("#monthlySubscriptionAmount").val());

    //   const totalMonthsWorked = Math.max(0, Math.ceil((retainmentDate - joiningDate) / (30 * 24 * 60 * 60 * 1000)));
    const yearsDiff = retainmentDate.getFullYear() - joiningDate.getFullYear();
const monthsDiff = retainmentDate.getMonth() - joiningDate.getMonth();
const daysDiff = retainmentDate.getDate() - joiningDate.getDate();

const totalMonthsWorked = yearsDiff * 12 + monthsDiff + (daysDiff > 0 ? 1 : 0);

console.log(totalMonthsWorked); 



      const initialTotalAmount = subscriptionMonths * monthlySubscriptionAmount;
      let totalAmount = initialTotalAmount;

      const subscriptionEndDate = new Date(joiningDate);
      subscriptionEndDate.setMonth(subscriptionEndDate.getMonth() + subscriptionMonths);

      let interestAmount;
      if (monthlySubscriptionAmount === 50) {
        interestAmount = 7535;
      } else if (monthlySubscriptionAmount === 70) {
        interestAmount = 10548;
      } else {
        interestAmount = 0;
      }

      $("#contributionPeriod").text(totalMonthsWorked);
      $("#subscriptionEndDate").text(subscriptionEndDate.toLocaleDateString("en-GB"));
      $("#subVal").text(" X " + monthlySubscriptionAmount);
      $("#CurrentBalance").text(interestAmount + totalAmount);
      $("#interest").text(interestAmount + " (standard)");
      $("#result").text(totalAmount.toFixed(0));
      // Clear existing rows in the installment table
      $("#installmentTable tr:gt(0)").remove();

      // Calculate the starting month for installment details
      const startMonth = subscriptionMonths + 1;

      // Loop to calculate and display installment details
      let currentBalance = totalAmount + interestAmount;
      let openingBalance = currentBalance; // Initialize opening balance
      let totalInterest = 0; // Initialize total interest amount

      for (let i = startMonth; i <= totalMonthsWorked ; i++) {
        const month = subscriptionEndDate.getMonth() + (i - startMonth);
        const year = subscriptionEndDate.getFullYear() + Math.floor(month / 12);
        const retainmentMonth = retainmentDate.getMonth() + 1;
        const retainmentYear = retainmentDate.getFullYear();

        const monthName = new Date(year, month % 12, 1).toLocaleDateString('en-us', { month: 'long' });
        let Totalamount = currentBalance + monthlySubscriptionAmount;

  let interestRate;

  //console.log(year,month,"year2 and month2");

  if (year < 2016 || (year === 2016 && month < 39)) { // March-2016 is index 2 (0-based)
    // console.log(year,month,"year2 and month2");
    interestRate = 0.087;
  } else if (year === 2016 && month >= 39 && month < 45) {
    interestRate = 0.081;
  } else if (((year === 2016 && month >= 45) &&(year ===2016 && month<=48) ) || ((year === 2017 && month >= 48) &&(year===2017 && month <51))) {
    interestRate = 0.08;
  } else if (year === 2017 && month >= 51 && month < 54) {
    interestRate = 0.079;
  } else if (year === 2017 && month >= 54 && month < 60) {
    interestRate = 0.078;
  } else if (year === 2018 && month >= 60 && month < 69) {
    interestRate = 0.076;
  } else if (((year === 2018 && month >= 69) && (year ===2018 && month <= 71)) || ((year === 2019 && month >= 72) && (year===2019 && month < 78))) {
    interestRate = 0.08;
  } else if (((year === 2019 && month >= 78) && (year ===2019 && month <= 83)) || ((year === 2020 && month >= 84) && (year ===2020 && month < 87))) {
    interestRate = 0.079;
  } else if ((year === 2020 && month >= 87) || (year === 2020 && month >= 95)) {
    interestRate = 0.071;
  } else if (year === 2021 && month >= 96) {
    interestRate = 0; // After January 2021, set interest rate to 0
  }


  let interestLAmount = Totalamount * interestRate * (1 / 12);
  let LoopCBalance = Totalamount + openingBalance; // Use opening balance here
  let crntbalance = Totalamount + interestLAmount;
  totalInterest += interestLAmount; // Add interest to total interest

  const newRow = `
    <tr>
      <td>${i}</td>
      <td>${monthName} - ${year}</td>
      <td>${openingBalance.toFixed(0)}</td>
      <td>${monthlySubscriptionAmount}</td>
      <td>${Totalamount.toFixed(0)}</td>
      <td>${(interestRate * 100).toFixed(1)}%</td>
      <td>${interestLAmount.toFixed(0)}</td>
      <td>${crntbalance.toFixed(0)}</td>
    </tr>
  `;

  $("#installmentTable").append(newRow);

  // Calculate next installment's values
  totalAmount += monthlySubscriptionAmount;
  openingBalance = crntbalance; // Update opening balance for the next iteration
  currentBalance = crntbalance;

  if (year > retainmentYear || (year === retainmentYear && month >= retainmentMonth)) {
    console.log(year, retainmentYear, month, retainmentMonth, "break");
        //   break; // Stop the loop if it meets the retainment date
        }

//   if (interestRate === 0 && year > subscriptionEndDate) {
//         //   break; // Stop the loop after January 2021
//         }
      }

      let intttl = totalInterest + interestAmount;
      // Display total interest amount
      $("#totalInterest").text(totalInterest.toFixed(0));
      $("#intttl").text(intttl.toFixed(0));
    });
  });
</script>
jquery 数组 循环 if 语句 逻辑运算符

评论


答: 暂无答案