I see that you want to improve your script in two ways:
Improve the spending strategy: Modify the calculation so that the available money is spent more efficiently, perhaps by maximizing the total number of units or the total value.
Fix the toggling: Ensure that the toggles for Gunmen, Craftsmen, and Missile Sites work correctly, and that the calculations adjust based on which units are selected.
Below is the improved version of your script, which addresses both issues.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Unit Purchase Calculator with Prioritized Value</title>
<style>
/* ... (Your existing CSS styles remain unchanged) ... */
</style>
</head>
<body>
<div class="container">
<h2>Unit Purchase Calculator</h2>
<!-- Available Money Input -->
<label for="money">Available Money:</label>
<input type="number" id="money" placeholder="Enter your available money" min="0">
<!-- Gunmen Category -->
<div class="category">
<div class="toggle-container">
<label class="switch">
<input type="checkbox" id="toggleGunmen" checked>
<span class="slider"></span>
</label>
<label for="toggleGunmen">Gunmen</label>
</div>
<div>
<input type="number" id="existingGunmen" placeholder="Existing Gunmen" min="0" value="0">
</div>
</div>
<!-- Craftsmen Category -->
<div class="category">
<div class="toggle-container">
<label class="switch">
<input type="checkbox" id="toggleCraftsmen" checked>
<span class="slider"></span>
</label>
<label for="toggleCraftsmen">Craftsmen</label>
</div>
<div>
<input type="number" id="existingCraftsmen" placeholder="Existing Craftsmen" min="0" value="0">
</div>
</div>
<!-- Missile Sites Category -->
<div class="category">
<div class="toggle-container">
<label class="switch">
<input type="checkbox" id="toggleMissileSites" checked>
<span class="slider"></span>
</label>
<label for="toggleMissileSites">Missile Sites</label>
</div>
<div>
<input type="number" id="existingMissileSites" placeholder="Existing Missile Sites" min="0" value="0">
</div>
</div>
<button onclick="calculatePurchases()">Calculate Maximum Purchases</button>
<div class="result hidden" id="result">
<h3>Maximum Purchases:</h3>
<div id="purchaseDetails"></div>
<div id="totalSpent"></div>
<div id="remainingMoney"></div>
</div>
<div class="warning hidden" id="warning"></div>
</div>
<script>
function calculatePurchases() {
// Reset warnings and results
document.getElementById('warning').classList.add('hidden');
document.getElementById('result').classList.add('hidden');
document.getElementById('purchaseDetails').innerHTML = '';
document.getElementById('totalSpent').innerHTML = '';
document.getElementById('remainingMoney').innerHTML = '';
// Get available money
let money = parseFloat(document.getElementById('money').value);
if (isNaN(money) || money < 0) {
displayWarning("Please enter a valid amount of money.");
return;
}
// Get existing units
let existingGunmen = parseInt(document.getElementById('existingGunmen').value) || 0;
let existingCraftsmen = parseInt(document.getElementById('existingCraftsmen').value) || 0;
let existingMissileSites = parseInt(document.getElementById('existingMissileSites').value) || 0;
// Check which units are selected
let isGunmenSelected = document.getElementById('toggleGunmen').checked;
let isCraftsmenSelected = document.getElementById('toggleCraftsmen').checked;
let isMissileSitesSelected = document.getElementById('toggleMissileSites').checked;
if (!isGunmenSelected && !isCraftsmenSelected && !isMissileSitesSelected) {
displayWarning("Please select at least one unit to perform the calculation.");
return;
}
const units = [];
if (isGunmenSelected) {
units.push({
name: 'Gunmen',
price: 2000,
existing: existingGunmen,
purchased: 0,
total: 0
});
}
if (isCraftsmenSelected) {
units.push({
name: 'Craftsmen',
price: 8000,
existing: existingCraftsmen,
purchased: 0,
total: 0
});
}
if (isMissileSitesSelected) {
units.push({
name: 'Missile Sites',
price: 12000,
existing: existingMissileSites,
purchased: 0,
total: 0
});
}
// Sort units by price ascending (cheapest first)
units.sort((a, b) => a.price - b.price);
let remainingMoney = money;
let totalSpent = 0;
// Purchase units starting from the cheapest
for (let unit of units) {
unit.purchased = Math.floor(remainingMoney / unit.price);
unit.total = unit.existing + unit.purchased;
remainingMoney -= unit.purchased * unit.price;
totalSpent += unit.purchased * unit.price;
}
// Display results
let purchaseResults = units.map(unit => {
return `${unit.name}: ${unit.purchased} additional (Total: ${unit.total})`;
});
document.getElementById('purchaseDetails').innerHTML = purchaseResults.join('<br>');
document.getElementById('totalSpent').innerHTML = `<strong>Total Spent:</strong> $${totalSpent.toLocaleString()}`;
document.getElementById('remainingMoney').innerHTML = `Remaining Money: $${remainingMoney.toLocaleString()}`;
document.getElementById('result').classList.remove('hidden');
}
function displayWarning(message) {
let warningDiv = document.getElementById('warning');
warningDiv.innerText = message;
warningDiv.classList.remove('hidden');
}
</script>
</body>
</html>
Explanation of Changes:
Fixed the Toggling:
The script now checks which units are selected by the user using the toggles.
If no units are selected, it displays a warning: "Please select at least one unit to perform the calculation."
The calculation only includes units that are toggled on.
Improved the Spending Strategy:
The units are now sorted by price in ascending order (from cheapest to most expensive).
The script purchases as many units as possible starting from the cheapest unit. This approach ensures that the available money is used to buy the maximum number of units possible.
For each unit, the script calculates how many can be purchased with the remaining money and updates the remaining money accordingly.
Removed Ratio Constraints:
The previous logic enforced ratios between Gunmen, Craftsmen, and Missile Sites (e.g., 1 Craftsman per 4 Gunmen). This might not be desired if the user wants flexibility in what units to purchase.
The improved script removes those constraints to allow the user to purchase units independently based on their toggles.
Updated Result Display:
The results now show the additional units purchased and the total units after including existing units.
The total money spent and remaining money are updated to reflect the new purchasing strategy.
Optional Enhancements:
Prioritization:
If you wish to prioritize certain units over others (regardless of price), you can modify the units array before sorting it. For example, you can add a priority property and sort based on that.
Reintroducing Ratios:
If you need to enforce certain ratios (e.g., you can only purchase Craftsmen if you have a certain number of Gunmen), you can add conditions after calculating the purchased units to adjust the quantities accordingly.
Note:
This improved script assumes that units can be purchased independently and that the goal is to maximize the total number of units purchased with the available money. Adjust the script further if you have specific requirements or constraints regarding the unit purchases.