Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Water Hardness Calculator #1772

Merged
merged 10 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Calculators/Water-Hardness-Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# <p align="center">Water Hardness Calculator</p>

## Description :-

Calculator helps users determine the hardness of their water supply. Water hardness is typically measured in parts per million (ppm) or grains per gallon (gpg), indicating the concentration of calcium and magnesium ions.

## Tech Stacks :-

- HTML
- CSS
- JavaScript

## Screenshots :-

![image](https://github.com/user-attachments/assets/a55c07cf-42ac-44fa-9385-09a4cab8aad0)
39 changes: 39 additions & 0 deletions Calculators/Water-Hardness-Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Water Hardness Calculator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Water Hardness Calculator</h1>
<form id="hardness-form">
<div class="input-group">
<label for="calcium">Calcium (Ca<sup>2+</sup>) in mg/L:</label>
<input type="number" id="calcium" required>
</div>
<div class="input-group">
<label for="magnesium">Magnesium (Mg<sup>2+</sup>) in mg/L:</label>
<input type="number" id="magnesium" required>
</div>
<div class="input-group">
<label for="unit">Select Unit:</label>
<select id="unit">
<option value="ppm">ppm</option>
<option value="gpg">gpg</option>
</select>
</div>
<button type="button" onclick="calculateHardness()">Calculate Hardness</button>
</form>
<div id="results">
<h2>Results</h2>
<p id="hardness"></p>
<p id="category"></p>
<p id="safety"></p>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
66 changes: 66 additions & 0 deletions Calculators/Water-Hardness-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
function calculateHardness() {
// Get input values
const calciumInput = document.getElementById('calcium');
const magnesiumInput = document.getElementById('magnesium');
const calcium = parseFloat(calciumInput.value);
const magnesium = parseFloat(magnesiumInput.value);
const unit = document.getElementById('unit').value;

// Validate input fields
let isValid = true;
let errorMessage = '';

if (isNaN(calcium) || calcium < 0) {
isValid = false;
errorMessage += 'Please enter a valid number for Calcium.\n';
calciumInput.classList.add('error');
} else {
calciumInput.classList.remove('error');
}

if (isNaN(magnesium) || magnesium < 0) {
isValid = false;
errorMessage += 'Please enter a valid number for Magnesium.\n';
magnesiumInput.classList.add('error');
} else {
magnesiumInput.classList.remove('error');
}

// Display error message if inputs are invalid
if (!isValid) {
alert(errorMessage);
return;
}

// Calculate hardness in ppm
const hardnessPPM = calcium * 2.5 + magnesium * 4.1;

// Convert ppm to gpg if necessary
let hardness = hardnessPPM;
if (unit === 'gpg') {
hardness = hardnessPPM / 17.1;
}

// Determine hardness category
let category = '';
let safety = '';
if (hardnessPPM < 60) {
category = 'Soft';
safety = 'Safe for all household uses.';
} else if (hardnessPPM < 120) {
category = 'Moderately Hard';
safety = 'Generally safe but may require water softening for some uses.';
} else if (hardnessPPM < 180) {
category = 'Hard';
safety = 'Can cause scale buildup; softening recommended for household use.';
} else {
category = 'Very Hard';
safety = 'Not suitable for household use without softening; can cause significant scale buildup.';
}

// Display results
document.getElementById('hardness').textContent = `Water Hardness: ${hardness.toFixed(2)} ${unit}`;
document.getElementById('category').textContent = `Category: ${category}`;
document.getElementById('safety').textContent = `Safety: ${safety}`;
}

64 changes: 64 additions & 0 deletions Calculators/Water-Hardness-Calculator/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}

.container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
text-align: center;
}

h1 {
margin-bottom: 20px;
}

.input-group {
margin-bottom: 15px;
text-align: left;
}

label {
display: block;
margin-bottom: 5px;
}

input, select {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
}

button {
width: 100%;
padding: 10px;
border: none;
border-radius: 4px;
background-color: #007bff;
color: white;
font-size: 16px;
cursor: pointer;
}

button:hover {
background-color: #0056b3;
}

#results {
margin-top: 20px;
text-align: left;
}
.error {
border: 2px solid red;
}

15 changes: 15 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4985,6 +4985,21 @@ <h3>Calculates number of vowels and consonants in a given paragraph.</h3>
</div>
</div>
<div class="box">
<div class="content">
<h2>Water Hardness Calculator</h2>
<h3>Determines the hardness of your water supply.</h3>
<div class="card-footer">
<a href="./Calculators/Water-Hardness-Calculator/index.html" target="_blank">
<button>Try Now</button>
</a>
<a href="https://github.com/mahek0620/CalcDiverse/tree/water-hardness/Calculators/Water-Hardness-Calculator" title="Source Code" target="_blank">
<img src="./assets/images/github.png" alt="Source Code">
</a>
</div>
</div>
</div>

<div class="box">
<div class="content">
<h2>Water Intake Calculator</h2>
<h3>Calculate daily water intake based on weight, activity, and weather.</h3>
Expand Down