[Python] creating table using JavaScript with respective increase but it only creates for 239...

Discussão em 'Python' iniciado por Stack, Janeiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I tried to solve problem through ai and used loop to control rows but all in vein. Problem is that I have a platform to work in which I need to calculate and calculating manually is so much tiring so I created code in python which works well as I want but I want to show it graphically so I changed code to JavaScript to show in table unlike in lists of python but this create resulte for 11 rows but I need minimum 50 rounds **In other words I need calculation which will show me what to add next from round one to 50 in form of table. Table include profit, amount in per round,total in all round,round no. **

    I tried below html code and wanted to get resulte of python code which is in end I summarize form through html.

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Calculator</title>

    <style>

    body {

    font-family: Arial, sans-serif;

    }

    h1 {

    text-align: center;

    }

    .input {

    background-color: #f7f7f7;

    padding: 2em;

    border: 1px solid #ddd;

    box-shadow: 0 0 10px rgba(0, 0, 0, 1);

    }

    .output {

    background-color: #f7f7f7;

    padding: 2em;

    border: 1px solid #ddd;

    box-shadow: 0 0 10px rgba(0, 0, 0, 1);

    }

    </style>


    <h1>Calculator</h1>

    <form class="input">

    <input type="number">

    <button onclick="calculateInningsAndProfit(); return false;">Calculate</button>

    </form>

    <form class="output">

    <table id="output-table">

    <thead>

    <tr>

    <th>Round no.</th>

    <th>Amount</th>

    <th>Total in</th>

    <th>Status</th>

    <th>Total out</th>

    <th>Profit</th>

    </tr>

    </thead>

    <tbody id="output-table-body">

    </tbody>

    </table>

    </form>



    <script>


    function calculateInningsAndProfit() {

    const amountForCalculation = parseInt(document.querySelector('input[type="number"]').value);

    const amount = 20;

    const addingAmount = 20;

    let totalInF = 0;

    let roundsNo = [];

    let amountsIn = [];

    let totalsIn = [];

    let profits = [];

    let round = 1;

    let addRow = true;



    while (addRow) {

    let maxProfitIn = amount * 11.4;

    let totalIn = totalInF + amount;

    let profit = maxProfitIn - totalIn;



    if (totalIn > amountForCalculation) {

    addRow = false;

    } else {

    totalsIn.push(totalIn);

    profits.push(profit);

    amountsIn.push(amount);

    totalInF = totalIn;



    if (totalIn < maxProfitIn) {

    // do nothing

    } else {

    amount += addingAmount;

    }



    round++;

    }

    }



    roundsNo = Array.from({length: totalsIn.length}, (_, i) => i + 1);



    const outputTableBody = document.getElementById('output-table-body');

    outputTableBody.innerHTML = '';



    for (let i = 0; i < roundsNo.length; i++) {

    const row = document.createElement('tr');

    row.innerHTML = `

    <td>${roundsNo}</td>

    <td>${amountsIn}</td>

    <td>${totalsIn}</td>

    <td></td>

    <td></td>

    <td>${profits}</td>

    `;

    outputTableBody.appendChild(row);

    }


    }

    </script>


    Rounds_no = []

    Amounts_in = []

    Totals_in = []

    Profits = []

    amount_for_calculation = 1000000

    amount = 20

    adding_amount = 20

    total_in_f = amount

    Totals_in.append(total_in_f)

    Profits.append(amount*11.4-total_in_f)

    Amounts_in.append(amount)

    while total_in_f < amount_for_calculation:

    last_total_in = Totals_in[-1]

    max_profit_in = amount * 11.4

    total_in = last_total_in + amount

    profit = max_profit_in - total_in



    if total_in > amount_for_calculation:

    break



    if total_in < max_profit_in:

    Totals_in.append(total_in)

    Profits.append(profit)

    Amounts_in.append(amount)

    else:

    amount += adding_amount

    max_profit_in = amount * 11.4

    total_in = last_total_in + amount

    profit = max_profit_in - total_in



    if total_in < max_profit_in:

    Totals_in.append(total_in)

    Profits.append(profit)

    Amounts_in.append(amount)



    total_in_f = total_in


    Rounds_no = list(range(1, len(Totals_in) + 1))

    print("Rounds_no:", Rounds_no)

    print("Amounts_in:", Amounts_in)

    print("Totals_in:", Totals_in)

    print("Profits:", Profits)

    Continue reading...

Compartilhe esta Página