-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmolarity_calculator.html
More file actions
244 lines (227 loc) · 10.7 KB
/
Copy pathmolarity_calculator.html
File metadata and controls
244 lines (227 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="A professional, clean, and responsive Molarity Calculator for chemistry students and professionals. Calculate molarity from mass, molar mass, and volume with ease.">
<title>Molarity Calculator - Chemistry Tools</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* Custom styles for better UI presentation */
body {
background-color: #f8fafc; /* A very light gray, almost white */
}
.saas-card {
background-color: white;
border-radius: 12px;
box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.07), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
transition: all 0.3s ease-in-out;
}
.saas-card:hover {
transform: translateY(-5px);
box-shadow: 0 20px 30px -10px rgba(0, 0, 0, 0.1);
}
.input-field {
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 12px;
width: 100%;
transition: border-color 0.2s, box-shadow 0.2s;
}
.input-field:focus {
outline: none;
border-color: #4f46e5;
box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.2);
}
.calculate-btn {
background-color: #4f46e5;
color: white;
font-weight: 600;
border-radius: 8px;
padding: 12px 24px;
width: 100%;
transition: background-color 0.3s;
}
.calculate-btn:hover {
background-color: #4338ca;
}
.result-box {
background-color: #ecfdf5;
border-left: 4px solid #10b981;
padding: 16px;
border-radius: 8px;
margin-top: 24px;
}
.error-message {
color: #ef4444;
font-size: 0.875rem;
margin-top: 8px;
}
</style>
</head>
<body class="flex items-center justify-center min-h-screen p-4">
<main class="max-w-2xl w-full">
<div class="saas-card p-8">
<!-- Header -->
<div class="text-center mb-8">
<h1 class="text-3xl font-bold text-gray-800">Molarity Calculator</h1>
<p class="text-gray-500 mt-2">Calculate the molarity of a solution from mass, molar mass, and volume.</p>
</div>
<!-- Calculator Form -->
<form id="molarity-form" onsubmit="return false;">
<div class="space-y-6">
<!-- Mass Input -->
<div>
<label for="mass" class="block text-sm font-medium text-gray-700 mb-1">Mass (g)</label>
<div class="relative">
<input type="number" id="mass" class="input-field pl-4" placeholder="e.g., 58.44" step="any">
<span class="absolute inset-y-0 right-4 flex items-center text-gray-500">g</span>
</div>
</div>
<!-- Molar Mass Input -->
<div>
<label for="molar-mass" class="block text-sm font-medium text-gray-700 mb-1">Molar Mass (g/mol)</label>
<div class="relative">
<input type="number" id="molar-mass" class="input-field pl-4" placeholder="e.g., 58.44" step="any">
<span class="absolute inset-y-0 right-4 flex items-center text-gray-500">g/mol</span>
</div>
</div>
<!-- Volume Input -->
<div>
<label for="volume" class="block text-sm font-medium text-gray-700 mb-1">Volume (L)</label>
<div class="relative">
<input type="number" id="volume" class="input-field pl-4" placeholder="e.g., 1" step="any">
<span class="absolute inset-y-0 right-4 flex items-center text-gray-500">L</span>
</div>
</div>
</div>
<!-- Calculate Button -->
<div class="mt-8">
<button id="calculate-btn" class="calculate-btn">Calculate</button>
</div>
</form>
<!-- Result and Error Display -->
<div id="result-container" class="mt-6"></div>
<!-- Demonstration Section (initially hidden) -->
<div id="demonstration-container" class="mt-6 hidden"></div>
<!-- Educational Note -->
<div class="text-center mt-8">
<p class="text-xs text-gray-400">For educational and laboratory estimation purposes only.</p>
</div>
</div>
<!-- Explanation Section Placeholder -->
<div class="mt-8 p-6 bg-white rounded-lg shadow-sm">
<h2 class="text-xl font-semibold text-gray-700">How to Calculate Molarity</h2>
<p class="text-gray-600 mt-4">
Molarity is a measure of the concentration of a solute in a solution. It is expressed in moles of solute per liter of solution. The formula is:
</p>
<p class="text-center font-mono text-lg bg-gray-100 p-4 rounded-md my-4">
Molarity (M) = (mass / molar mass) / volume
</p>
<p class="text-gray-600">
This tool simplifies the calculation, ensuring quick and accurate results for your experiments or studies. Simply input your values and let the calculator do the rest.
</p>
</div>
</main>
<script>
// --- DOM Element Selection ---
const massInput = document.getElementById('mass');
const molarMassInput = document.getElementById('molar-mass');
const volumeInput = document.getElementById('volume');
const calculateBtn = document.getElementById('calculate-btn');
const resultContainer = document.getElementById('result-container');
const demonstrationContainer = document.getElementById('demonstration-container');
// --- Event Listener ---
calculateBtn.addEventListener('click', () => {
// Clear previous results or errors
resultContainer.innerHTML = '';
demonstrationContainer.innerHTML = '';
demonstrationContainer.classList.add('hidden');
// --- Input Retrieval and Validation ---
const mass = parseFloat(massInput.value);
const molarMass = parseFloat(molarMassInput.value);
const volume = parseFloat(volumeInput.value);
let isValid = true;
let errorMessage = '';
if (isNaN(mass) || mass <= 0) {
errorMessage = 'Please enter a valid, positive number for mass.';
isValid = false;
} else if (isNaN(molarMass) || molarMass <= 0) {
errorMessage = 'Please enter a valid, positive number for molar mass.';
isValid = false;
} else if (isNaN(volume) || volume <= 0) {
errorMessage = 'Please enter a valid, positive number for volume.';
isValid = false;
}
// --- Calculation and Display ---
if (isValid) {
// Formula: Molarity = (mass / molar mass) / volume
const moles = mass / molarMass;
const molarity = moles / volume;
// Display the result
displayResult(molarity);
// Display the demonstration
displayDemonstration(mass, molarMass, volume, moles, molarity);
} else {
// Display an error message
displayError(errorMessage);
}
});
/**
* Displays the calculated molarity in a formatted result box.
* @param {number} molarity - The calculated molarity value.
*/
function displayResult(molarity) {
const resultHTML = `
<div class="result-box">
<p class="text-sm text-green-700 font-semibold">Calculated Molarity:</p>
<p class="text-3xl font-bold text-green-800 mt-1">
${molarity.toFixed(3)} <span class="text-xl font-medium">mol/L</span>
</p>
</div>
`;
resultContainer.innerHTML = resultHTML;
}
/**
* Displays a step-by-step demonstration of the calculation.
* @param {number} mass
* @param {number} molarMass
* @param {number} volume
* @param {number} moles
* @param {number} molarity
*/
function displayDemonstration(mass, molarMass, volume, moles, molarity) {
const demonstrationHTML = `
<div class="bg-gray-100 p-6 rounded-lg">
<h3 class="text-lg font-semibold text-gray-800 mb-4">Calculation Demonstration</h3>
<div class="space-y-4 text-gray-700 font-mono">
<p class="flex justify-between"><span><strong>Formula:</strong></span> <span>M = (mass / MM) / V</span></p>
<hr class="border-gray-300"/>
<p class="text-sm text-gray-500">Step 1: Calculate moles of solute</p>
<p class="flex justify-between"><span>Moles = mass / Molar Mass</span> <span>${mass.toFixed(2)} g / ${molarMass.toFixed(2)} g/mol</span></p>
<p class="flex justify-end font-bold">= ${moles.toFixed(4)} mol</p>
<hr class="border-gray-300"/>
<p class="text-sm text-gray-500">Step 2: Calculate molarity</p>
<p class="flex justify-between"><span>Molarity = Moles / Volume</span> <span>${moles.toFixed(4)} mol / ${volume.toFixed(2)} L</span></p>
<p class="flex justify-end font-bold">= ${molarity.toFixed(4)} mol/L</p>
</div>
</div>
`;
demonstrationContainer.innerHTML = demonstrationHTML;
demonstrationContainer.classList.remove('hidden');
}
/**
* Displays an error message inside the result container.
* @param {string} message - The error message to display.
*/
function displayError(message) {
const errorHTML = `
<div class="p-4 bg-red-100 border-l-4 border-red-500 rounded-lg">
<p class="error-message text-red-700 font-medium">${message}</p>
</div>
`;
resultContainer.innerHTML = errorHTML;
}
</script>
</body>
</html>