To modify your script to prioritize having equal value, meaning that gunmen/4 = craftsmen and gunmen/6 = missile site, we need to adjust the calculations in your JavaScript code to enforce these constraints.
Here's how you can do it:
-
Determine the Ratios and Unit Multiples:
- Since
gunmen/4 = craftsmen, total gunmen must be a multiple of 4.
- Since
gunmen/6 = missile sites, total gunmen must be a multiple of 6.
- To satisfy both conditions simultaneously, the total number of gunmen must be a multiple of the least common multiple (LCM) of 4 and 6, which is 12.
-
Adjust Calculations Based on Selected Units:
- If both craftsmen and missile sites are selected:
- Total gunmen =
12 * n, where n is a non-negative integer.
- Total craftsmen =
3 * n.
- Total missile sites =
2 * n.
- The cost per
n is TotalCost(n) = 72,000 * n.
- If only craftsmen are selected:
- Total gunmen =
4 * n.
- Total craftsmen =
n.
- The cost per
n is TotalCost(n) = 10,000 * n.
- If only missile sites are selected:
- Total gunmen =
6 * n.
- Total missile sites =
n.
- The cost per
n is TotalCost(n) = 24,000 * n.
-
Implement the Logic in JavaScript:
- Calculate the maximum possible
n based on the available money and constraints.
- Use a loop to decrease
n until all purchased units are non-negative and satisfy the constraints.
- Calculate the total cost and ensure it does not exceed the available money.
- Update the result to display the number of units purchased and the remaining money.
Below is the modified code incorporating these changes:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Unit Purchase Calculator with Prioritized Value</title>
<style>
/* Your existing styles */
</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 disabled>
<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;
// Get toggle states
let toggleGunmen = document.getElementById('toggleGunmen').checked; // Always true
let toggleCraftsmen = document.getElementById('toggleCraftsmen').checked;
let toggleMissileSites = document.getElementById('toggleMissileSites').checked;
// Check that Gunmen is selected (always true in this case)
if (!toggleGunmen) {
displayWarning("Gunmen must be selected to perform the calculation.");
return;
}
const priceGunmen = 2000;
const priceCraftsmen = 8000;
const priceMissileSites = 12000;
let availableMoney = money;
// Determine the ratios and unit multiples
let gunmenMultiple = 1; // Default when only gunmen are selected
let perN_Cost = 0;
if (toggleCraftsmen && toggleMissileSites) {
gunmenMultiple = 12;
perN_Cost = 72000;
} else if (toggleCraftsmen) {
gunmenMultiple = 4;
perN_Cost = (4 * priceGunmen) + (1 * priceCraftsmen);
} else if (toggleMissileSites) {
gunmenMultiple = 6;
perN_Cost = (6 * priceGunmen) + (1 * priceMissileSites);
} else {
// Only gunmen selected
gunmenMultiple = 1;
perN_Cost = priceGunmen;
}
// Initialize n_max based on available money
let n_max = Math.floor(availableMoney / perN_Cost);
let n_min = 0;
let best_n = -1;
let totalCost = 0;
let purchasedGunmen = 0;
let purchasedCraftsmen = 0;
let purchasedMissileSites = 0;
for (let n = n_max; n >= n_min; n--) {
// Calculate total units
let totalGunmen = gunmenMultiple * n;
let Gp = totalGunmen - existingGunmen;
if (Gp < 0) continue; // Cannot purchase negative units
let totalCraftsmen = existingCraftsmen;
let totalMissileSites = existingMissileSites;
let Cp = 0;
let Mp = 0;
if (toggleCraftsmen) {
if (toggleMissileSites) {
// Both craftsmen and missile sites selected
totalCraftsmen = (totalGunmen / 4);
totalMissileSites = (totalGunmen / 6);
} else {
// Only craftsmen selected
totalCraftsmen = (totalGunmen / 4);
}
} else if (toggleMissileSites) {
// Only missile sites selected
totalMissileSites = (totalGunmen / 6);
}
Cp = totalCraftsmen - existingCraftsmen;
Mp = totalMissileSites - existingMissileSites;
if (Cp < 0 || Mp < 0) continue; // Cannot purchase negative units
// Check if total units are integers
if (!Number.isInteger(totalCraftsmen) || !Number.isInteger(totalMissileSites)) continue;
// Calculate total cost
totalCost = (Gp * priceGunmen) + (Cp * priceCraftsmen) + (Mp * priceMissileSites);
if (totalCost <= availableMoney) {
// Found the maximum n satisfying the constraints
best_n = n;
purchasedGunmen = Gp;
purchasedCraftsmen = Cp;
purchasedMissileSites = Mp;
break;
}
}
if (best_n === -1) {
displayWarning("Unable to purchase units within the given constraints and budget.");
return;
}
// Remaining money
let remainingMoney = availableMoney - totalCost;
// Display results
let purchaseResults = [];
if (toggleGunmen) {
purchaseResults.push(`Gunmen: ${purchasedGunmen} additional (Total: ${existingGunmen + purchasedGunmen})`);
}
if (toggleCraftsmen) {
purchaseResults.push(`Craftsmen: ${purchasedCraftsmen} additional (Total: ${existingCraftsmen + purchasedCraftsmen})`);
}
if (toggleMissileSites) {
purchaseResults.push(`Missile Sites: ${purchasedMissileSites} additional (Total: ${existingMissileSites + purchasedMissileSites})`);
}
document.getElementById('purchaseDetails').innerHTML = purchaseResults.join('<br>');
document.getElementById('totalSpent').innerHTML = `<strong>Total Spent:</strong> $${totalCost.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:
- Ratios and Multiples: Adjusted the calculation to enforce the ratios
gunmen/4 = craftsmen and gunmen/6 = missile site by determining the appropriate multiples based on selected units.
- Per N Cost Calculation: Calculated the cost per
n unit based on the selected units.
- Loop Over N: Implemented a loop to find the maximum
n starting from n_max down to n_min, ensuring that all purchased units are non-negative and satisfy the constraints.
- Total Cost Calculation: Calculated the total cost for the purchased units and compared it against the available money.
- Result Display: Updated the result display to show the number of additional units purchased and the remaining money.
Note:
- The Gunmen toggle is now disabled and always checked since the calculation requires gunmen to be selected.
- The script handles different scenarios based on whether craftsmen and missile sites are selected or not.
- The constraints ensure that the ratios and existing units are considered in the calculation.
Testing the Script:
Try different values for available money and existing units to see how the calculator adjusts the number of units purchased while maintaining the specified ratios.
Example:
- Available Money: $500,000
- Existing Units:
- Gunmen: 0
- Craftsmen: 0
- Missile Sites: 0
- Selected Units: All selected
Result:
- Maximum Purchases:
- Gunmen: 72 additional (Total: 72)
- Craftsmen: 18 additional (Total: 18)
- Missile Sites: 12 additional (Total: 12)
- Total Spent: $432,000
- Remaining Money: $68,000
This result ensures that the ratios gunmen/4 = craftsmen (72/4 = 18) and gunmen/6 = missile sites (72/6 = 12) are maintained.