MediaWiki:SmoothiesDebtCalculator.js

From Roat Pkz
Revision as of 10:55, 18 July 2026 by Hefner (talk | contribs)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
console.log("Smoothies calculator loaded");
(function () {

function parseAmount(value) {

    value = value
        .toLowerCase()
        .replace(/,/g,"")
        .trim();


    if (value.endsWith("b")) {
        return Number(value.slice(0,-1)) * 1000000000;
    }

    if (value.endsWith("m")) {
        return Number(value.slice(0,-1)) * 1000000;
    }

    if (value.endsWith("k")) {
        return Number(value.slice(0,-1)) * 1000;
    }

    return Number(value) || 0;
}



function calculateDebt() {

    let pkp =
        parseAmount(
            document.getElementById("pkp-amount").value
        );


    let dp =
        parseAmount(
            document.getElementById("dp-price").value
        )
        *
        parseAmount(
            document.getElementById("dp-amount").value
        );


    let credits =
        parseAmount(
            document.getElementById("credits-price").value
        )
        *
        parseAmount(
            document.getElementById("credits-amount").value
        );


    let total = pkp + dp + credits;


    document.getElementById("total").innerHTML =
        Math.floor(total).toLocaleString() + " PKP";

}



document.addEventListener("DOMContentLoaded", function(){

    let inputs = document.querySelectorAll(
        ".debt-item input"
    );


    inputs.forEach(function(input){

        input.addEventListener(
            "input",
            calculateDebt
        );

    });

});

console.log("Inputs found:", document.querySelectorAll(".debt-item input").length);
})();