Sure! I'll create an HTML page with embedded JavaScript that allows users to input their available money and the number of gunmen, craftsmen, and missile sites they already own. The script will then calculate the maximum additional units they can purchase for each category while ensuring that the total amount spent on each category remains equal.
Assumptions:
- Equal Spending on Each Category: The user’s available money is divided equally among the three categories (Gunmen, Craftsmen, Missile Sites).
- Adjusting for Existing Units: The existing number of units reduces the amount available for purchasing additional units.
- Integer Purchases: Users can only purchase whole units; fractional purchases are not allowed.
Here's the complete HTML code implementing the above logic:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Unit Purchase Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 40px;
background-color: #f4f4f4;
}
.container {
background-color: #fff;
padding: 20px 30px 30px 30px;
border-radius: 8px;
max-width: 500px;
margin: auto;
box-shadow: 0 2px 5px rgba(0,0,0,0.3);
}
h2 {
text-align: center;
color: #333;
}
label {
display: block;
margin-top: 15px;
color: #555;
}
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: #28a745;
border: none;
color: white;
font-size: 16px;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #218838;
}
.result {
margin-top: 20px;
padding: 15px;
background-color: #e9ecef;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container">
<h2>Unit Purchase Calculator</h2>
<label for="money">Available Money:</label>
<input type="number" id="money" placeholder="Enter your available money" min="0">
<label for="existingGunmen">Existing Gunmen:</label>
<input type="number" id="existingGunmen" placeholder="Number of gunmen already owned" min="0" value="0">
<label for="existingCraftsmen">Existing Craftsmen:</label>
<input type="number" id="existingCraftsmen" placeholder="Number of craftsmen already owned" min="0" value="0">
<label for="existingMissileSites">Existing Missile Sites:</label>
<input type="number" id="existingMissileSites" placeholder="Number of missile sites already owned" min="0" value="0">
<button onclick="calculatePurchases()">Calculate Maximum Purchases</button>
<div class="result" id="result" style="display:none;">
<h3>Maximum Purchases:</h3>
<p id="maxGunmen"></p>
<p id="maxCraftsmen"></p>
<p id="maxMissileSites"></p>
</div>
</div>
<script>
function calculatePurchases() {
// Prices
const PRICE_GUNMAN = 2000;
const PRICE_CRAFTSMAN = 8000;
const PRICE_MISSILE_SITE = 12000;
// Get user inputs
let money = parseFloat(document.getElementById('money').value);
let existingGunmen = parseInt(document.getElementById('existingGunmen').value) || 0;
let existingCraftsmen = parseInt(document.getElementById('existingCraftsmen').value) || 0;
let existingMissileSites = parseInt(document.getElementById('existingMissileSites').value) || 0;
if (isNaN(money) || money < 0) {
alert("Please enter a valid amount of money.");
return;
}
// Equal spending per category
let spendPerCategory = money / 3;
// Calculate maximum additional units for each category
let maxAdditionalGunmen = Math.floor(spendPerCategory / PRICE_GUNMAN);
let maxAdditionalCraftsmen = Math.floor(spendPerCategory / PRICE_CRAFTSMAN);
let maxAdditionalMissileSites = Math.floor(spendPerCategory / PRICE_MISSILE_SITE);
// Display results
document.getElementById('maxGunmen').innerText = `Gunmen: ${maxAdditionalGunmen} additional (Total: ${existingGunmen + maxAdditionalGunmen})`;
document.getElementById('maxCraftsmen').innerText = `Craftsmen: ${maxAdditionalCraftsmen} additional (Total: ${existingCraftsmen + maxAdditionalCraftsmen})`;
document.getElementById('maxMissileSites').innerText = `Missile Sites: ${maxAdditionalMissileSites} additional (Total: ${existingMissileSites + maxAdditionalMissileSites})`;
document.getElementById('result').style.display = 'block';
}
</script>
</body>
</html>
How It Works:
-
User Inputs:
- Available Money: The total money the user has to spend.
- Existing Units: The number of gunmen, craftsmen, and missile sites the user already owns.
-
Calculation Logic:
- Equal Division: The available money is divided equally among the three categories.
- Maximum Additional Units: For each category, the script calculates how many more units can be purchased with the allocated budget.
- Total Units: The existing units are added to the additional units to provide the total count.
-
Result Display:
- The results are shown below the button, displaying the maximum additional units that can be purchased and the new total for each category.
Example:
- Available Money: 72000
- Existing Gunmen: 5
- Existing Craftsmen: 2
- Existing Missile Sites: 1
Calculation:
- Spend per category: 72000 / 3 = 24000
- Gunmen: 24000 / 2000 = 12 additional (Total: 5 + 12 = 17)
- Craftsmen: 24000 / 8000 = 3 additional (Total: 2 + 3 = 5)
- Missile Sites: 24000 / 12000 = 2 additional (Total: 1 + 2 = 3)
Notes:
- This approach ensures that the spending on each category is equal, maintaining balanced growth.
- If the user’s available money isn't perfectly divisible by 3, the calculation rounds down to ensure no overspending.
- The existing units are purely additive and don't affect the spending distribution.
Feel free to customize the styling or logic as per your specific requirements!