Sure! Below is a complete HTML page that allows users to input their budget and calculates the maximum number of gunmen, craftsmen, and missile sites they can purchase. The calculation ensures that the total value of each type of unit is equal, meaning:
- Gunmen: 2000 each
- Craftsmen: 8000 each
- Missile Sites: 12000 each
Given the constraint that the total value spent on each type is equal, the number of units bought will be proportional to their costs.
How It Works:
-
Equal Value Constraint:
- Let the total value allocated to each unit type be
V.
- Number of Gunmen (
x) = V / 2000
- Number of Craftsmen (
y) = V / 8000
- Number of Missile Sites (
z) = V / 12000
-
Total Budget Constraint:
V (Gunmen) + V (Craftsmen) + V (Missile Sites) = 3V <= Budget
- Therefore,
V = Budget / 3
-
Calculations:
- Calculate
V by dividing the budget by 3.
- Calculate the number of each unit by dividing
V by their respective costs.
- Ensure that the number of units is an integer (using
Math.floor).
HTML Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Unit Purchase Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 20px;
}
.container {
background-color: #fff;
padding: 20px 30px 30px 30px;
border-radius: 8px;
max-width: 500px;
margin: auto;
box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}
h1 {
text-align: center;
color: #333;
}
label {
display: block;
margin-top: 15px;
font-weight: bold;
}
input[type="number"] {
width: 100%;
padding: 8px;
margin-top: 5px;
box-sizing: border-box;
border-radius: 4px;
border: 1px solid #ccc;
}
button {
margin-top: 20px;
width: 100%;
padding: 10px;
background-color: #007BFF;
border: none;
color: white;
font-size: 16px;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
.result {
margin-top: 20px;
padding: 15px;
background-color: #e9ffe9;
border: 1px solid #b2ffb2;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container">
<h1>Unit Purchase Calculator</h1>
<label for="budget">Enter Your Budget:</label>
<input type="number" id="budget" placeholder="Enter amount in dollars" min="0">
<button onclick="calculateUnits()">Calculate</button>
<div id="result" class="result" style="display:none;"></div>
</div>
<script>
function calculateUnits() {
const budgetInput = document.getElementById('budget').value;
const budget = parseFloat(budgetInput);
if (isNaN(budget) || budget < 0) {
alert('Please enter a valid positive number for the budget.');
return;
}
// Costs
const costGunman = 2000;
const costCraftsman = 8000;
const costMissile = 12000;
// Equal value allocated to each unit type
const totalValuePerType = budget / 3;
// Number of units
const numGunmen = Math.floor(totalValuePerType / costGunman);
const numCraftsmen = Math.floor(totalValuePerType / costCraftsman);
const numMissiles = Math.floor(totalValuePerType / costMissile);
// Adjust in case total exceeds budget due to flooring
const totalSpent = (numGunmen * costGunman) + (numCraftsmen * costCraftsman) + (numMissiles * costMissile);
if (totalSpent > budget) {
alert('Error in calculation. Please try again.');
return;
}
// Display the result
const resultDiv = document.getElementById('result');
resultDiv.style.display = 'block';
resultDiv.innerHTML = `
<h3>Purchase Summary:</h3>
<p><strong>Gunmen:</strong> ${numGunmen}</p>
<p><strong>Craftsmen:</strong> ${numCraftsmen}</p>
<p><strong>Missile Sites:</strong> ${numMissiles}</p>
<p><strong>Total Spent:</strong> $${totalSpent.toLocaleString()}</p>
<p><strong>Remaining Budget:</strong> $${(budget - totalSpent).toLocaleString()}</p>
`;
}
</script>
</body>
</html>
Explanation of the Code:
-
HTML Structure:
- A simple form with an input field for the budget and a button to trigger the calculation.
- A
div to display the results after calculation.
-
Styling:
- Basic CSS for a clean and user-friendly interface.
- Responsive design to ensure it looks good on different devices.
-
JavaScript Function (calculateUnits):
- Input Validation: Checks if the entered budget is a valid positive number.
- Calculation:
- Divides the budget equally among the three unit types.
- Calculates the maximum number of each unit type using
Math.floor to ensure whole units are purchased.
- Result Display:
- Shows the number of each unit, total spent, and remaining budget.
- If the total spent exceeds the budget due to any calculation error, it alerts the user.
How to Use:
- Open the HTML File: Save the above code in a file named
index.html.
- Run in Browser: Open the file using any web browser.
- Input Budget: Enter the amount of money you have in the input field.
- Calculate: Click the "Calculate" button to see the maximum number of gunmen, craftsmen, and missile sites you can purchase while ensuring the total value of each type remains equal.
Example:
- Budget: $60,000
- Calculation:
- Total Value per Type = 60,000/3=20,000
- Gunmen = 20,000/2,000 = 10
- Craftsmen = 20,000/8,000 = 2.5 → 2 (since we use
Math.floor)
- Missile Sites = 12,000 ≈ 1.666 → 1
Feel free to customize the styling or functionality as per your requirements!