.calculator-container {
font-family: sans-serif;
max-width: 800px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
background-color: #f9f9f9;
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.calculator-inputs {
flex: 1;
min-width: 300px;
}
.calculator-result {
flex: 1;
min-width: 300px;
background-color: #e9ecef;
padding: 15px;
border-radius: 5px;
}
.input-group {
margin-bottom: 15px;
}
.input-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #333;
}
.input-group input[type="text"] {
width: calc(100% – 20px);
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1em;
}
button {
background-color: #007bff;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1em;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #0056b3;
}
#result h3 {
margin-top: 0;
color: #333;
}
#result p {
margin-bottom: 10px;
color: #555;
line-height: 1.6;
}
code {
background-color: #e0e0e0;
padding: 2px 5px;
border-radius: 3px;
}
function calculateRateLaw() {
var concA1 = parseFloat(document.getElementById("concA1").value);
var concB1 = parseFloat(document.getElementById("concB1").value);
var rate1 = parseFloat(document.getElementById("rate1").value);
var concA2 = parseFloat(document.getElementById("concA2").value);
var concB2 = parseFloat(document.getElementById("concB2").value);
var rate2 = parseFloat(document.getElementById("rate2").value);
var concA3 = parseFloat(document.getElementById("concA3").value);
var concB3 = parseFloat(document.getElementById("concB3").value);
var rate3 = parseFloat(document.getElementById("rate3").value);
var orderA = NaN;
var orderB = NaN;
var rateConstant = NaN;
var rateLawExpression = "Could not determine.";
if (isNaN(concA1) || isNaN(concB1) || isNaN(rate1) ||
isNaN(concA2) || isNaN(concB2) || isNaN(rate2) ||
isNaN(concA3) || isNaN(concB3) || isNaN(rate3)) {
document.getElementById("reactionOrderA").innerHTML = "Error: Please enter valid numbers for all inputs.";
document.getElementById("reactionOrderB").innerHTML = "";
document.getElementById("rateConstant").innerHTML = "";
document.getElementById("rateLawExpression").innerHTML = "";
return;
}
// Determine order of A using experiments 1 and 2 (where [B] is constant)
if (concB1 === concB2 && concA1 !== concA2 && concA1 > 0 && concA2 > 0 && rate1 > 0 && rate2 > 0) {
orderA = Math.round(Math.log(rate2 / rate1) / Math.log(concA2 / concA1));
} else if (concB1 === concB3 && concA1 !== concA3 && concA1 > 0 && concA3 > 0 && rate1 > 0 && rate3 > 0) {
orderA = Math.round(Math.log(rate3 / rate1) / Math.log(concA3 / concA1));
} else if (concB2 === concB3 && concA2 !== concA3 && concA2 > 0 && concA3 > 0 && rate2 > 0 && rate3 > 0) {
orderA = Math.round(Math.log(rate3 / rate2) / Math.log(concA3 / concA2));
}
// Determine order of B using experiments 1 and 3 (where [A] is constant)
if (concA1 === concA3 && concB1 !== concB3 && concB1 > 0 && concB3 > 0 && rate1 > 0 && rate3 > 0) {
orderB = Math.round(Math.log(rate3 / rate1) / Math.log(concB3 / concB1));
} else if (concA1 === concA2 && concB1 !== concB2 && concB1 > 0 && concB2 > 0 && rate1 > 0 && rate2 > 0) {
orderB = Math.round(Math.log(rate2 / rate1) / Math.log(concB2 / concB1));
} else if (concA2 === concA3 && concB2 !== concB3 && concB2 > 0 && concB3 > 0 && rate2 > 0 && rate3 > 0) {
orderB = Math.round(Math.log(rate3 / rate2) / Math.log(concB3 / concB2));
}
// Calculate rate constant (k) using one of the experiments (e.g., experiment 1)
// Ensure orderA and orderB are valid before calculating k
if (!isNaN(orderA) && !isNaN(orderB) && concA1 > 0 && concB1 > 0 && rate1 > 0) {
rateConstant = rate1 / (Math.pow(concA1, orderA) * Math.pow(concB1, orderB));
} else if (!isNaN(orderA) && !isNaN(orderB) && concA2 > 0 && concB2 > 0 && rate2 > 0) {
rateConstant = rate2 / (Math.pow(concA2, orderA) * Math.pow(concB2, orderB));
} else if (!isNaN(orderA) && !isNaN(orderB) && concA3 > 0 && concB3 > 0 && rate3 > 0) {
rateConstant = rate3 / (Math.pow(concA3, orderA) * Math.pow(concB3, orderB));
}
if (!isNaN(orderA)) {
document.getElementById("reactionOrderA").innerHTML = "Reaction order with respect to A (m): " + orderA;
} else {
document.getElementById("reactionOrderA").innerHTML = "Could not determine reaction order for A. Ensure one reactant concentration is constant while the other varies between two experiments.";
}
if (!isNaN(orderB)) {
document.getElementById("reactionOrderB").innerHTML = "Reaction order with respect to B (n): " + orderB;
} else {
document.getElementById("reactionOrderB").innerHTML = "Could not determine reaction order for B. Ensure one reactant concentration is constant while the other varies between two experiments.";
}
if (!isNaN(rateConstant)) {
document.getElementById("rateConstant").innerHTML = "Rate Constant (k): " + rateConstant.toExponential(3) + " M
-" + (orderA + orderB – 1) + "s
-1";
} else {
document.getElementById("rateConstant").innerHTML = "Could not calculate rate constant. Ensure valid orders and concentrations are provided.";
}
if (!isNaN(orderA) && !isNaN(orderB) && !isNaN(rateConstant)) {
rateLawExpression = "Rate = " + rateConstant.toExponential(3) + "[A]
" + orderA + "[B]
" + orderB + "";
}
document.getElementById("rateLawExpression").innerHTML = rateLawExpression;
}