A unit rate is a rate where the denominator is exactly one. It is used to compare different quantities. For example, if you are comparing prices of two different sized packages of the same item, you would calculate the unit price (price per ounce, price per pound, etc.) to determine which is the better deal. If you are comparing speeds of two vehicles, you would calculate the unit speed (miles per hour, kilometers per hour) to see which is faster. The formula for calculating a unit rate is:
Unit Rate = Total Quantity / Total Amount
function calculateUnitRate() {
var totalQuantityInput = document.getElementById("totalQuantity");
var totalAmountInput = document.getElementById("totalAmount");
var resultDiv = document.getElementById("result");
var totalQuantity = parseFloat(totalQuantityInput.value);
var totalAmount = parseFloat(totalAmountInput.value);
if (isNaN(totalQuantity) || isNaN(totalAmount)) {
resultDiv.innerHTML = "Please enter valid numbers for both quantities.";
return;
}
if (totalAmount === 0) {
resultDiv.innerHTML = "The total amount cannot be zero.";
return;
}
var unitRate = totalQuantity / totalAmount;
resultDiv.innerHTML = "The Unit Rate is: " + unitRate.toFixed(2);
}
.unit-rate-calculator {
font-family: Arial, sans-serif;
border: 1px solid #ccc;
padding: 20px;
border-radius: 5px;
max-width: 400px;
margin: 20px auto;
background-color: #f9f9f9;
}
.unit-rate-calculator h2 {
text-align: center;
color: #333;
margin-bottom: 15px;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
.form-group input[type="number"] {
width: calc(100% – 12px);
padding: 8px;
border: 1px solid #ccc;
border-radius: 3px;
box-sizing: border-box;
}
.unit-rate-calculator button {
display: block;
width: 100%;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
margin-top: 20px;
}
.unit-rate-calculator button:hover {
background-color: #45a049;
}
#result {
margin-top: 20px;
padding: 10px;
background-color: #e0e0e0;
border-radius: 3px;
text-align: center;
font-weight: bold;
color: #333;
}