Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

<!

DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Milk Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
}
table {
border-collapse: collapse;
margin-bottom: 20px;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: center;
}
#totalMilk, #totalIncrement, #totalDecrement, #resultX, #finalMilk, #amount
{
font-weight: bold;
}
#resultBox {
margin-top: 20px;
border: 1px solid black;
padding: 10px;
width: 200px;
}
</style>
<script>
function addRow() {
var table = document.getElementById("calculatorTable");
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);

// Add cells to the new row


var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);

// Populate cells with input fields


cell1.innerHTML = rowCount;
cell2.innerHTML = '<input type="number" id="milk' + rowCount + '">';
cell3.innerHTML = '<input type="number" id="sample' + rowCount + '">';
cell4.innerHTML = '<input type="text" id="decrement' + rowCount + '">';
cell5.innerHTML = '<input type="text" id="increment' + rowCount + '">';

// Update numbering for existing rows


for (var i = 1; i < rowCount; i++) {
table.rows[i].cells[0].innerHTML = i;
}
}

function deleteRow() {
var table = document.getElementById("calculatorTable");
var rowCount = table.rows.length;
if (rowCount > 2) {
table.deleteRow(rowCount - 1);
}
}

function calculate() {
var table = document.getElementById("calculatorTable");
var rowCount = table.rows.length;
var totalMilk = 0;
var totalIncrement = 0;
var totalDecrement = 0;
var totalX = 0;
var finalMilk = 0;
var amount = 0;

for (var i = 1; i < rowCount; i++) {


var milk = parseFloat(document.getElementById('milk' + i).value);
var sample = parseFloat(document.getElementById('sample' +
i).value);
var decrement = 0;
var increment = 0;

// Update the decrement and increment based on the sample value


if (sample < 65) {
decrement = (65 - sample) * milk;
totalDecrement += decrement;
} else {
increment = (sample - 65) * milk;
totalIncrement += increment;
}

// Update the decrement and increment input fields


document.getElementById('decrement' + i).value =
decrement.toFixed(2);
document.getElementById('increment' + i).value =
increment.toFixed(2);

// Calculate total milk (without roundoff)


totalMilk += milk;
}

// Calculate total X
totalX = ((totalDecrement - totalIncrement) * 1.5) / 100;

// Calculate final milk


finalMilk = totalMilk - totalX;

// Get rate from input field


var rate = parseFloat(document.getElementById('rate').value);

// Calculate amount
amount = finalMilk * rate;

// Display results
document.getElementById('totalMilk').innerHTML = totalMilk.toFixed(2);
document.getElementById('totalDecrement').innerHTML =
totalDecrement.toFixed(2);
document.getElementById('totalIncrement').innerHTML =
totalIncrement.toFixed(2);
document.getElementById('resultX').innerHTML = totalX.toFixed(2);
document.getElementById('finalMilk').innerHTML = finalMilk.toFixed(2);
document.getElementById('amount').innerHTML = amount.toFixed(2);
}
</script>
</head>
<body>
<h1>Calculator</h1>
<table id="calculatorTable">
<tr>
<th>Numbering</th>
<th>Milk</th>
<th>Sample</th>
<th>Decrement</th>
<th>Increment</th>
</tr>
<!-- Initial row -->
<tr>
<td>1</td>
<td><input type="number" id="milk1"></td>
<td><input type="number" id="sample1"></td>
<td><input type="text" id="decrement1"></td>
<td><input type="text" id="increment1"></td>
</tr>
</table>
<label for="rate">Rate:</label>
<input type="number" id="rate" placeholder="Enter rate">
<button onclick="addRow()">Add Row</button>
<button onclick="deleteRow()">Delete Row</button>
<button onclick="calculate()">Calculate</button>
<div id="resultBox">
<p>Total Milk (without roundoff): <span id="totalMilk">0</span></p>
<p>Total Decrement: <span id="totalDecrement">0</span></p>
<p>Total Increment: <span id="totalIncrement">0</span></p>
<p>Result X: <span id="resultX">0</span></p>
<p>Final Milk: <span id="finalMilk">0</span></p>
<p>Amount: <span id="amount">0</span></p>
</div>
</body>
</html>

You might also like