Javascript code for VA Disability Rating Calculator


On my VA Disability Rating Calculator post, there is program written in Javascript that calculates both the total by value and actual total of any amount of disabilities with a rating awarded by the Department of Veteran’s Affairs. The page running the code is here:

And the source code itself can be easily copied from here:

<button style="background-color:black">
    <input type="button" id="btnAdd" onclick="newVal()" value="Add Disability by %" style:"backgroundcolor:black="" color:white"="">
    <hr>
    <div id="dvContainer"></div>
    <label id="lblBottom" style="color:white"></label>
    <script type="text/javascript">
        var amounts = [
            { disabilityAmount: 10, Num: "10%" },
            { disabilityAmount: 20, Num: "20%" },
            { disabilityAmount: 30, Num: "30%" },
            { disabilityAmount: 40, Num: "40%" },
            { disabilityAmount: 50, Num: "50%" },
            { disabilityAmount: 60, Num: "60%" },
            { disabilityAmount: 70, Num: "70%" },
            { disabilityAmount: 80, Num: "80%" },
            { disabilityAmount: 90, Num: "90%" }
        ];
        function newVal()
        {
          AddDropDownList();
          RunningTotal();
        }
        function AddDropDownList()
        {
          //Create a DropDownList element.
          var ddlAmounts = document.createElement("SELECT");
          ddlAmounts.style.backgroundColor = "black";

          //Add the Options to the DropDownList.
          for (var i = 0; i < amounts.length; i++)
          {
              var option = document.createElement("OPTION");

              //Set Num in Text part.
              option.innerHTML = amounts[i].Num;

              //Set disabilityAmount in Value part.
              option.value = amounts[i].disabilityAmount;

              //Add the Option element to DropDownList.
              ddlAmounts.options.add(option);
          }

          //Reference the container DIV.
          var dvContainer = document.getElementById("dvContainer")

          //Add the DropDownList to DIV.
          var div = document.createElement("DIV");
          div.appendChild(ddlAmounts);

          var btnRemove = document.createElement("button");
          btnRemove.style.color = "white";
          btnRemove.appendChild(document.createTextNode('Remove'));
          btnRemove.onclick = function()
          {
            ddlAmounts.remove();
            btnRemove.remove();
            RunningTotal();
          };

          //Add the Remove Buttton to DIV.
          div.appendChild(btnRemove);

          //Add the DIV to the container DIV.
          dvContainer.appendChild(div);
          ddlAmounts.addEventListener("change", function()
          {
            RunningTotal();
          });
        };
        function RunningTotal()
        {
          var total = document.getElementById('lblBottom');
          var runningTotal = 0;
          var leftOver;
          var current;
          var actualTotal;
          var allDropDowns = Array.prototype.slice.call(document.querySelectorAll("select"));
          if (allDropDowns.length > 0)
          {
            allDropDowns.forEach(select =>
            {
              leftOver = 100 - runningTotal;
              current = parseInt(select.value);
              runningTotal += (leftOver / 100) * current;
              total.innerHTML = "Total by value = " + runningTotal + "<br>";
              if (parseInt(runningTotal) % 10 < 5)
              {
               actualTotal = Math.floor(runningTotal / 10) * 10;
              }
              else
              {
               actualTotal = Math.ceil(runningTotal / 10) * 10;
              }
              total.innerHTML += "Actual total = " + actualTotal;
            });
          }
          else
          {
            total.innerHTML = "";
          }
        }
    </script>
</button>
,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: