Socialify

Folder ..

Viewing script.js
63 lines (58 loc) • 2.3 KB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const numInputs = document.querySelectorAll(".afnum");
const calculateForm = document.getElementById("calculate");
const result = document.getElementById("result");

function commaSeparateNumber(val) {
    while (/(\d+)(\d{3})/.test(val.toString())) {
        val = val.toString().replace(/(\d+)(\d{3})/, "$1" + "," + "$2");
    }
    return val;
}

numInputs.forEach((input) => {
    input.addEventListener("keyup", (e) => {
        const value = e.target.value;
        const formattedValue = commaSeparateNumber(value.replace(/\D/g, ""));
        e.target.value = formattedValue;
    });
});

calculateForm.addEventListener("submit", (e) => {
    e.preventDefault();
    const buyPrice = document.getElementById("buyprice");
    const sellPrice = document.getElementById("sellprice");
    const reducedTaxes = document.getElementById("taxrates");
    const buyTaxRate = 0.05;
    const sellTaxRate = reducedTaxes.checked ? 0.03 : 0.05;
    const buyPriceValue = parseInt(buyPrice.value.replace(/\D/g, ""));
    const sellPriceValue = parseInt(sellPrice.value.replace(/\D/g, ""));
    const buyTax = Math.ceil(buyPriceValue * buyTaxRate);
    const sellTax = Math.ceil(sellPriceValue * sellTaxRate);
    const profit = sellPriceValue - buyPriceValue - buyTax - sellTax;

    const resultDisplay = `
        <table class="result-display">
            <tr>
                <td>Buying Cost:</td>
                <td>${commaSeparateNumber(buyPriceValue)} Gil</td>
            </tr>
            <tr>
                <td>Buying Tax:</td>
                <td>${commaSeparateNumber(buyTax)} Gil</td>
            </tr>
            <tr>
                <td>Selling Cost:</td>
                <td>${commaSeparateNumber(sellPriceValue)} Gil</td>
            </tr>
            <tr>
                <td>Selling Tax:</td>
                <td>${commaSeparateNumber(sellTax)} Gil</td>
            </tr>
            <tr>
                <td>${profit > 0 ? "Profit" : "Loss"}:</td>
                <td><span class="${profit > 0 ? "profit" : "loss"}">${commaSeparateNumber(Math.abs(profit))}</span> Gil</td>
                </td>
            </tr>
        </table>
    `;

    const resultElement = document.createElement("div");
    resultElement.innerHTML = resultDisplay;
    result.innerHTML = "";
    result.appendChild(resultElement);
});