<div class="">Convert <span id="convertFromTxt"></span> to <span id="convertToTxt"></span></div>
<div class="convertion-wrapper">
<div class="selection-wrapper">
- <select name="unitFrom" id="unitFrom">
+ <select name="tempUnitFrom" id="tempUnitFrom">
<option value="Celcius" selected>°C</option>
<option value="Fahrenheit">°F</option>
<option value="Kelvin">K</option>
</select>
- <select name="unitTo" id="unitTo">
+ <select name="tempUnitTo" id="tempUnitTo">
<option value="Celcius">°C</option>
<option value="Fahrenheit" selected>°F</option>
<option value="Kelvin">K</option>
<input type="text" name="userInput" id="userInput" onkeyup="calculate(this.value)">
</div>
</div>
- <div id="result">0°C = 32.00°F
- <!-- <span id="resultFrom">0</span><span id="resultUnitFrom">°C</span> =
- <span id ="resultTo">32</span><span id="resultUnitTo">°F</span> -->
- </div>
+ <div id="result">0°C = 32.00°F</div>
<hr>
<div id="solution"></div>
</main>
-const tempUnitFrom = document.getElementById("unitFrom");
-const tempUnitTo = document.getElementById("unitTo");
+const tempUnitFrom = document.getElementById("tempUnitFrom");
+const tempUnitTo = document.getElementById("tempUnitTo");
const result = document.getElementById("result");
const fromTxt = document.getElementById("convertFromTxt");
const toTxt = document.getElementById("convertToTxt");
+const userInput = document.getElementById("userInput");
const convert = (num, from, to) => {
let result = "";
return result;
}
-const updateUI = () => {
-
+const updateOutputTxt = (from, to, num) => {
+ const units = {
+
+ }
+ return `${userInput.value}°${from} = ${num}°${to}`;
}
-const calculate = (value) => {
- const from = tempUnitFrom.value;
- const to = tempUnitTo.value;
+const updateConversionTxt = (from, to) => {
+ fromTxt.innerText = from;
+ toTxt.innerText = to;
+ calculate(userInput);
+}
- if (from === "Celcius") {
- if (to === "Fahrenheit") {
- fromTxt.innerText = "Celcius";
- toTxt.innerText = "Fahrenheit";
- } else if (to === "Kelvin") {
- fromTxt.innerText = "Celcius";
- toTxt.innerText = "Kelvin";
- } else {
- fromTxt.innerText = "Celcius";
- toTxt.innerText = "Celcius";
- }
- } else if (from === "Fahrenheit") {
- if (to === "Celcius") {
- fromTxt.innerText = "Fahrenheit";
- toTxt.innerText = "Celcius";
- } else if (to === "Kelvin") {
- fromTxt.innerText = "Fahrenheit";
- toTxt.innerText = "Kelvin";
- } else {
- fromTxt.innerText = "Fahrenheit";
- toTxt.innerText = "Fahrenheit";
- }
- } else if (from === "Kelvin") {
- if (to === "Celcius") {
- fromTxt.innerText = "Kelvin";
- toTxt.innerText = "Celcius";
- } else if (to === "Fahrenheit") {
- fromTxt.innerText = "Kelvin";
- toTxt.innerText = "Fahrenheit";
- } else {
- fromTxt.innerText = "Kelvin";
- toTxt.innerText = "Kelvin";
- }
- }
+const onDropdownChange = () => {
+ const tempFrom = tempUnitFrom.value;
+ const tempTo = tempUnitTo.value;
+ updateConversionTxt(tempFrom, tempTo);
+}
+
+tempUnitFrom.addEventListener("change", onDropdownChange);
+tempUnitTo.addEventListener("change", onDropdownChange);
- const noSpace = value.replace(/[^\d]/g, ""); //remove whitespaces and non-numerical characters
+const calculate = (value) => {
+ const noSpace = value.replace(/[^\d.]/g, ""); //remove whitespaces and non-numerical characters
const parsedInt = parseInt(noSpace);
+ const from = tempUnitFrom.value;
+ const to = tempUnitTo.value;
if (!parsedInt || isNaN(parsedInt)) {
- result.innerHTML = `0°C = 32.00°F`;
+ result.innerHTML = updateOutputTxt(from, to, convert(parsedInt, from, to));
return;
} else {
- document.getElementById("result").innerHTML = convert(parsedInt, from, to);
- // updateUI();
+ document.getElementById("result").innerHTML = updateOutputTxt(from, to, convert(parsedInt, from, to));
}
}
-document.addEventListener("DOMContentLoaded", calculate);
\ No newline at end of file
+document.addEventListener("DOMContentLoaded", onDropdownChange);
\ No newline at end of file