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 Moles Grams Calculator #1960

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
60 changes: 60 additions & 0 deletions Calculators/Moles-Grams-Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# <p align="center">Moles Grams Calculator</p>

## Description :-

This project is a web-based calculator that allows users to:

- Convert **Moles to Grams**.
- Convert **Grams to Moles**.
- Switch between calculation modes seamlessly.

## Tech Stacks :-

- HTML
- CSS
- JavaScript

## Features :-

- **Dynamic Mode Switching**: Toggle between "Moles to Grams" and "Grams to Moles" calculations.
- **Responsive Design**: User-friendly interface that works on all screen sizes.
- **Real-Time Results**: Instant calculation results based on input values.
- **Background Styling**: Beautiful background image to enhance user experience.

## How to Use :-

1. **Default Mode (Moles to Grams)**:

- Enter the number of moles and the molar mass.
- Click the "Calculate" button to get the result.

2. **Switch Mode (Grams to Moles)**:

- Click the "Switch to Grams to Moles" button.
- Enter the grams and the molar mass.
- Click the "Calculate" button to get the result.

3. **Clear Inputs**:
- Switch modes or refresh the page to clear inputs.

## Example Calculations :-

### Moles to Grams:
- **Input**:
- Moles: `2`
- Molar Mass: `18`
- **Output**:
- `2 moles × 18 g/mol = 36 grams`

### Grams to Moles:
- **Input**:
- Grams: `36`
- Molar Mass: `18`
- **Output**:
- `36 grams ÷ 18 g/mol = 2 moles`

## Screenshots :-

![image](https://github.com/user-attachments/assets/08771d4d-0c7e-487d-8f21-67fc977d3ab1)

![image](https://github.com/user-attachments/assets/550079ed-4989-4a89-9d8b-13d5b4dc8cfc)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions Calculators/Moles-Grams-Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Moles Grams Calculator</title>
</head>

<body>
<div class="calculator">
<h1 id="calc-title">Moles to Grams Calculator</h1>

<label for="input1" id="label1">Enter Moles:</label>
<input type="number" id="input1" placeholder="Enter number of moles">

<label for="input2" id="label2">Enter Molar Mass (g/mol):</label>
<input type="number" id="input2" placeholder="Enter molar mass">

<button onclick="calculate()">Calculate</button>
<div class="result" id="result"></div>

<button class="switch-btn" onclick="toggleMode()">
Switch to Grams to Moles
</button>
</div>

<script src="script.js"></script>
</body>

</html>
42 changes: 42 additions & 0 deletions Calculators/Moles-Grams-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
let mode = 'moles-to-grams'; // Default mode

function calculate() {
const input1 = parseFloat(document.getElementById('input1').value);
const input2 = parseFloat(document.getElementById('input2').value);

if (isNaN(input1) || isNaN(input2)) {
document.getElementById('result').innerText = 'Please enter valid numbers for both fields.';
return;
}

let result;
if (mode === 'moles-to-grams') {
result = input1 * input2; // Moles to grams
document.getElementById('result').innerText = `${input1} moles x ${input2} g/mol = ${result.toFixed(2)} grams`;
} else {
result = input1 / input2; // Grams to moles
document.getElementById('result').innerText = `${input1} grams ÷ ${input2} g/mol = ${result.toFixed(2)} moles`;
}
}

function toggleMode() {
mode = mode === 'moles-to-grams' ? 'grams-to-moles' : 'moles-to-grams';

// Update UI based on mode
if (mode === 'moles-to-grams') {
document.getElementById('calc-title').innerText = 'Moles to Grams Calculator';
document.getElementById('label1').innerText = 'Enter Moles:';
document.getElementById('label2').innerText = 'Enter Molar Mass (g/mol):';
document.querySelector('.switch-btn').innerText = 'Switch to Grams to Moles';
} else {
document.getElementById('calc-title').innerText = 'Grams to Moles Calculator';
document.getElementById('label1').innerText = 'Enter Grams:';
document.getElementById('label2').innerText = 'Enter Molar Mass (g/mol):';
document.querySelector('.switch-btn').innerText = 'Switch to Moles to Grams';
}

// Clear result and inputs
document.getElementById('result').innerText = '';
document.getElementById('input1').value = '';
document.getElementById('input2').value = '';
}
71 changes: 71 additions & 0 deletions Calculators/Moles-Grams-Calculator/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
body {
font-family: Arial, sans-serif;
background-image: url('assets/background.jpg');
background-size: cover;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100%;
min-height: 100vh;
}

.calculator {
background: rgba(255, 255, 255, 0.9);
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
width: 350px;
text-align: center;
}

h1 {
font-size: 24px;
margin-bottom: 20px;
}

label {
font-size: 16px;
margin: 10px 0;
display: block;
}

input {
width: calc(100% - 20px);
padding: 10px;
margin-bottom: 20px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 4px;
}

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

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

.switch-btn {
background-color: #28a745;
}

.switch-btn:hover {
background-color: #1e7e34;
}

.result {
margin-top: 20px;
font-size: 18px;
font-weight: bold;
color: #333;
}
6 changes: 6 additions & 0 deletions calculators.json
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,12 @@
"link": "./Calculators/Molecular-Weight-Calculator/index.html",
"source": "https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/Molecular-Weight-Calculator"
},
{
"title": "Moles Grams Calculator",
"description": "Converts between moles and grams using the molar mass of a substance.",
"link": "./Calculators/Moles-Grams-Calculator/index.html",
"source": "https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/Moles-Grams-Calculator"
},
{
"title": "Momentum Calculator",
"description": "Calculates the momentum of the body using its mass and velocity.",
Expand Down
Loading