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

Enhanced the UI of Temperature Calculator #1746

Merged
merged 8 commits into from
Aug 8, 2024
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
25 changes: 12 additions & 13 deletions Calculators/Temperature-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<body>
<div class="container">
<div id="temp-conv">
<label class="labelId">Convert From : </label><br />
<label class="labelId">Convert From:</label><br />
<hr />
<input type="radio" value="celsius" name="fromUnit" />
<label>Celsius</label><br />
Expand All @@ -31,36 +31,35 @@
<label>Delisle</label><br /><br />
</div>
<div class="temp-conv">
<label class="labelId">Convert To : </label><br />
<label class="labelId">Convert To:</label><br />
<hr />
<input type="radio" value="celsius" data-symbol="C" name="toUnit" />
<input type="radio" value="celsius" name="toUnit" />
<label>Celsius</label><br />
<input type="radio" value="fahrenheit" data-symbol="F" name="toUnit" />
<input type="radio" value="fahrenheit" name="toUnit" />
<label>Fahrenheit</label><br />
<input type="radio" value="kelvin" data-symbol="K" name="toUnit" />
<input type="radio" value="kelvin" name="toUnit" />
<label>Kelvin</label><br />
<input type="radio" value="rankine" data-symbol="R" name="toUnit" />
<input type="radio" value="rankine" name="toUnit" />
<label>Rankine</label><br />
<input type="radio" value="romer" data-symbol="Rø" name="toUnit" />
<input type="radio" value="romer" name="toUnit" />
<label>Rømer</label><br />
<input type="radio" value="reaumur" data-symbol="Ré" name="toUnit" />
<input type="radio" value="reaumur" name="toUnit" />
<label>Réaumur</label><br />
<input type="radio" value="newton" data-symbol="N" name="toUnit" />
<input type="radio" value="newton" name="toUnit" />
<label>Newton</label><br />
<input type="radio" value="delisle" data-symbol="D" name="toUnit" />
<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" />

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

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

</html>
</html>
61 changes: 58 additions & 3 deletions Calculators/Temperature-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,51 @@
document.getElementById("submitButton").onclick = function () {
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(
Expand All @@ -15,7 +62,8 @@ document.getElementById("submitButton").onclick = function () {

const sourceUnit = sourceUnitElement.value;
const targetUnit = targetUnitElement.value;
const targetUnitSymbol = targetUnitElement.dataset.symbol;

const originalTemp = temp;

// we use kelvin as the base temp, this removes the need to account for possible combination of selections
var converter = {
Expand Down Expand Up @@ -51,9 +99,16 @@ document.getElementById("submitButton").onclick = function () {

if (sourceUnit != "kelvin") temp = converter[sourceUnit].toKelvin(temp);
if (targetUnit != "kelvin") temp = converter[targetUnit].fromKelvin(temp);
displayMessage(`${temp}°${targetUnitSymbol}`);
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);
};
}
79 changes: 45 additions & 34 deletions Calculators/Temperature-Calculator/style.css
Original file line number Diff line number Diff line change
@@ -1,72 +1,83 @@
body {
background-color: #449f6c;
background: linear-gradient(135deg, #ff7e5f, #feb47b);
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: #fff;
}

.container {
border-top-left-radius: 5px;
border-top-right-radius: 5px;
background-color: #93cfc9;
padding: 20px;
width: 500px;
margin: auto;
padding-top: 20px;
margin-top: 36px;
text-align: center;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
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: justify;
text-align: left;
}

.labelId {
margin-left: 64px;
font-weight: bold;
color: #f8f8f8;
}

input[type="radio"] {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
margin-right: 8px;
}

input[type="text"],
select {
input[type="text"] {
width: 100%;
padding: 12px 20px;
padding: 12px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
border: none;
border-radius: 8px;
box-sizing: border-box;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
font-size: 16px;
background-color: #f8f8f8;
color: #333;
}

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

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

#resultId {
background-color: #93cfc9;
width: 500px;
background-color: #fff;
color: #333;
width: 100%;
max-width: 600px;
padding: 20px;
margin: auto;
margin: 20px auto 0;
text-align: center;
padding-top: 0px;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
border-bottom-right-radius: 5px;
border-bottom-left-radius: 5px;
border-radius: 12px;
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}

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