Skip to content

Temperature Calculator #200

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

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
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions Calculators/Temperature Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!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>Temperature Calculator</title>
</head>

<body>
<div class="container">
<div id="temp-conv">
<label class="labelId">Convert From:</label><br />
<hr />
<input type="radio" value="celsius" name="fromUnit" />
<label>Celsius</label><br />
<input type="radio" value="fahrenheit" name="fromUnit" />
<label>Fahrenheit</label><br />
<input type="radio" value="kelvin" name="fromUnit" />
<label>Kelvin</label><br />
<input type="radio" value="rankine" name="fromUnit" />
<label>Rankine</label><br />
<input type="radio" value="romer" name="fromUnit" />
<label>Rømer</label><br />
<input type="radio" value="reaumur" name="fromUnit" />
<label>Réaumur</label><br />
<input type="radio" value="newton" name="fromUnit" />
<label>Newton</label><br />
<input type="radio" value="delisle" name="fromUnit" />
<label>Delisle</label><br /><br />
</div>
<div class="temp-conv">
<label class="labelId">Convert To:</label><br />
<hr />
<input type="radio" value="celsius" name="toUnit" />
<label>Celsius</label><br />
<input type="radio" value="fahrenheit" name="toUnit" />
<label>Fahrenheit</label><br />
<input type="radio" value="kelvin" name="toUnit" />
<label>Kelvin</label><br />
<input type="radio" value="rankine" name="toUnit" />
<label>Rankine</label><br />
<input type="radio" value="romer" name="toUnit" />
<label>Rømer</label><br />
<input type="radio" value="reaumur" name="toUnit" />
<label>Réaumur</label><br />
<input type="radio" value="newton" name="toUnit" />
<label>Newton</label><br />
<input type="radio" value="delisle" name="toUnit" />
<label>Delisle</label><br /><br />
</div>
</div>
<div id="resultId">
<label>Enter a Temperature</label>
<input type="text" id="textBox" />

<div id="result"></div>
<button id="submitButton" type="submit">Submit</button><br />
</div>
</body>

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

</html>
114 changes: 114 additions & 0 deletions Calculators/Temperature Calculator/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const fromUnitElements = document.querySelectorAll('input[name="fromUnit"]');
const toUnitElements = document.querySelectorAll('input[name="toUnit"]');
const submitButton = document.getElementById("submitButton");

fromUnitElements.forEach(element => {
element.addEventListener('change', handleUnitChange);
element.addEventListener('change', () => {
if (isSubmitted) {
debounce(updateResult, 600)();
}
});
});

// debouncing function to minimize function calls
toUnitElements.forEach(element => {
element.addEventListener('change', () => {
if (isSubmitted) {
debounce(updateResult, 600)();
}
});
});

// unit change handling function to display cconversions with selected unit
function handleUnitChange() {
const selectedFromUnit = document.querySelector('input[name="fromUnit"]:checked').value;

toUnitElements.forEach(element => {
if (element.value === selectedFromUnit) {
element.disabled = true;
} else {
element.disabled = false;
}
});
}

let debounceTimeout;
let isSubmitted = false;
let callCount = 0;

submitButton.onclick = function () {
updateResult();
isSubmitted = true;
}

function updateResult() {
// callCount++;
// console.log(`updateResult called ${callCount} times`); // Log the call count

let temp = Number(document.getElementById("textBox").value);

const sourceUnitElement = document.querySelector(
'input[name="fromUnit"]:checked'
);
const targetUnitElement = document.querySelector(
'input[name="toUnit"]:checked'
);

if (!sourceUnitElement || !targetUnitElement) {
displayMessage("You need to choose the Temperature units");
return;
}

const sourceUnit = sourceUnitElement.value;
const targetUnit = targetUnitElement.value;

const originalTemp = temp;

// we use kelvin as the base temp, this removes the need to account for possible combination of selections
var converter = {
celsius: {
toKelvin: (temp) => temp + 273.15,
fromKelvin: (temp) => temp - 273.15,
},
fahrenheit: {
toKelvin: (temp) => ((temp - 32) * 5) / 9 + 273.15,
fromKelvin: (temp) => ((temp - 273.15) * 9) / 5 + 32,
},
rankine: {
toKelvin: (temp) => (temp * 5) / 9,
fromKelvin: (temp) => temp * 1.8,
},
romer: {
toKelvin: (temp) => (temp - 7.5) / 0.525 + 273.15,
fromKelvin: (temp) => ((temp - 273.15) * 21) / 40 + 7.5,
},
reaumur: {
toKelvin: (temp) => (temp * 4) / 5 + 273.15,
fromKelvin: (temp) => (temp - 273.15) * 0.8,
},
newton: {
toKelvin: (temp) => (temp * 100) / 33 + 273.15,
fromKelvin: (temp) => (temp - 273.15) * 0.33,
},
delisle: {
toKelvin: (temp) => 373.15 - (temp * 2) / 3,
fromKelvin: (temp) => (temp - 273.15) * 1.5 - 100.0,
},
};

if (sourceUnit != "kelvin") temp = converter[sourceUnit].toKelvin(temp);
if (targetUnit != "kelvin") temp = converter[targetUnit].fromKelvin(temp);
displayMessage(`${originalTemp} degrees ${sourceUnit} is ${temp.toFixed(2)} degrees ${targetUnit}`);
};

function displayMessage(string) {
document.getElementById("result").innerHTML = string;
}

function debounce(func, delay) {
return function(...args) {
clearTimeout(debounceTimeout);
debounceTimeout = setTimeout(() => func.apply(this, args), delay);
};
}
83 changes: 83 additions & 0 deletions Calculators/Temperature Calculator/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
body {
background: linear-gradient(135deg, #0714a0, #e87bfe);
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
height: 100vh;
color: #ffffff;
}

.container {
border-radius: 12px;
background: linear-gradient(135deg, #00c6ff, #0072ff);
padding: 30px;
width: 100%;
max-width: 600px;
margin: 20px;
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 20px;
text-align: left;
}

.labelId {
font-weight: bold;
color: #f8f8f8;
}

input[type="radio"] {
margin-right: 8px;
}

input[type="text"] {
width: 100%;
padding: 12px;
margin: 8px 0;
border: none;
border-radius: 8px;
box-sizing: border-box;
font-size: 16px;
background-color: #f8f8f8;
color: #333333;
}

button[type="submit"] {
width: 100%;
background-color: #1736b3;
color: white;
padding: 14px;
margin: 8px 0;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s, transform 0.3s;
}

button[type="submit"]:hover {
background-color: #cd1928;
transform: scale(1.03);
}

#resultId {
background-color: #ffffff;
color: #333333;
width: 100%;
max-width: 600px;
padding: 20px;
margin: 20px auto 0;
text-align: center;
border-radius: 12px;
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}

#result {
font-weight: bold;
margin-top: 10px;
color: #333333;
}