const toTxt = document.getElementById("convertToTxt");
const userInput = document.getElementById("userInput");
-const convert = (num, from, to) => {
+const convert = (num, from, to) => { //converts the num to the temp unit assigned
let result = "";
if (from === "Celcius") {
}
}
- return result;
+ return result.toFixed(2);
}
-const updateOutputTxt = (from, to, num) => {
+const updateOutputTxt = (from, to, num) => { //updates the output text
const units = {
-
+ "Celcius": "°C",
+ "Fahrenheit": "°F",
+ "Kelvin": "K"
}
- return `${userInput.value}°${from} = ${num}°${to}`;
+ const noSpace = userInput.value.replace(/[^\d.]/g, "");
+ const fromUnit = units[from];
+ const toUnit = units[to];
+
+ return `${noSpace}${fromUnit} = ${num}${toUnit}`;
}
-const updateConversionTxt = (from, to) => {
+const updateConversionTxt = (from, to) => { //updates the text above the conversion
fromTxt.innerText = from;
toTxt.innerText = to;
calculate(userInput);
}
-const onDropdownChange = () => {
+const onDropdownChange = () => { //executes if user change the option on the select element
const tempFrom = tempUnitFrom.value;
const tempTo = tempUnitTo.value;
updateConversionTxt(tempFrom, tempTo);
}
+//checks changes on the select element
tempUnitFrom.addEventListener("change", onDropdownChange);
tempUnitTo.addEventListener("change", onDropdownChange);
+//executes when user input a number
const calculate = (value) => {
const noSpace = value.replace(/[^\d.]/g, ""); //remove whitespaces and non-numerical characters
const parsedInt = parseInt(noSpace);
}
}
-document.addEventListener("DOMContentLoaded", onDropdownChange);
\ No newline at end of file
+document.addEventListener("DOMContentLoaded", onDropdownChange); //run once on page onload
\ No newline at end of file