]> git repositories - temperature-converter.git/commitdiff
im already halfway, just saving because i dont know what to do next
authorametoresu <mail@tenkyuu.dev>
Mon, 23 Dec 2024 13:59:46 +0000 (21:59 +0800)
committerametoresu <mail@tenkyuu.dev>
Mon, 23 Dec 2024 13:59:46 +0000 (21:59 +0800)
index.html
script.js [new file with mode: 0644]
styles.css [new file with mode: 0644]

index dbd4901745f1dac7120357eec81f8c108515afeb..235fa560b8cbef8420c9d14b7ee03eaad511004f 100644 (file)
@@ -6,15 +6,47 @@
   <meta name="author" content="ametoresu">
   <meta name="description" content="converts temperatures from Celcius, Fahrenheit, and Kelvin.">
   <meta name="robots" content="index, follow">
-  <title>Temperature Converter by ametoresu</title>
+  <title>Temperature Converter</title>
   <link rel="icon" type="image/ico" href="/assets/icons/favicon.ico">
   <meta property="og:type" content="website">
-  <meta property="og:title" content="Temperature Converter by ametoresu">
+  <meta property="og:title" content="Temperature Converter">
   <meta property="og:description" content="a web application that converts temperatures on various units.">
   <meta property="og:image" content="/assets/pfp/profile.jpg">
   <meta property="og:url" content="https://tenkyuu.dev">
   <meta name="twitter:card" content="summary_large_image">
+  <link rel="stylesheet" href="styles.css">
 <body>
-  
+  <header></header>
+  <main>
+    <div class="summary">
+      <h1>Temperature Converter</h1>
+      <p>This web application allows users to convert temperatures between various units, including Celsius, Fahrenheit, and Kelvin. It provides a straightforward interface for quick and accurate conversions.</p>
+    </div>
+    <div class="converter-ctn">
+      <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">
+            <option value="Celcius" selected>&deg;C</option>
+            <option value="Fahrenheit">&deg;F</option>
+            <option value="Kelvin">K</option>
+          </select>
+          <select name="unitTo" id="unitTo">
+            <option value="Celcius">&deg;C</option>
+            <option value="Fahrenheit" selected>&deg;F</option>
+            <option value="Kelvin">K</option>
+          </select>
+        </div>
+        <input type="text" name="userInput" id="userInput" onkeyup="calculate(this.value)">
+      </div>
+    </div>
+    <div id="result">0&deg;C &equals; 32.00&deg;F
+      <!-- <span id="resultFrom">0</span><span id="resultUnitFrom">&deg;C</span> &equals; 
+      <span id ="resultTo">32</span><span id="resultUnitTo">&deg;F</span> -->
+    </div>
+    <hr>
+    <div id="solution"></div>
+  </main>
+  <script src="script.js"></script>
 </body>
 </html>
\ No newline at end of file
diff --git a/script.js b/script.js
new file mode 100644 (file)
index 0000000..b3d8231
--- /dev/null
+++ b/script.js
@@ -0,0 +1,94 @@
+const tempUnitFrom = document.getElementById("unitFrom");
+const tempUnitTo = document.getElementById("unitTo");
+const result = document.getElementById("result");
+const fromTxt = document.getElementById("convertFromTxt");
+const toTxt = document.getElementById("convertToTxt");
+
+const convert = (num, from, to) => {
+  let result = "";
+
+  if (from === "Celcius") {
+    if (to === "Fahrenheit") {
+      result = (num * 1.8) + 32;
+    } else if (to === "Kelvin") {
+      result = num + 273.15;
+    } else {
+      result = num;
+    }
+  } else if (from === "Fahrenheit") {
+    if (to === "Celcius") {
+      result = (num - 32) / (9 / 5);
+    } else if (to === "Kelvin") {
+      result = (num + 459.67) / 1.8;
+    } else {
+      result = num;
+    }
+  } else if (from === "Kelvin") {
+    if (to === "Celcius") {
+      result = num - 273.15;
+    } else if (to === "Fahrenheit") {
+      result = (num * 1.8) - 459.67;
+    } else {
+      result = num;
+    }
+  }
+
+  return result;
+}
+
+const updateUI = () => {
+  
+}
+
+const calculate = (value) => {
+  const from = tempUnitFrom.value;
+  const to = tempUnitTo.value;
+
+  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 noSpace = value.replace(/[^\d]/g, ""); //remove whitespaces and non-numerical characters
+  const parsedInt = parseInt(noSpace);
+
+  if (!parsedInt || isNaN(parsedInt)) {
+    result.innerHTML = `0&deg;C &equals; 32.00&deg;F`;
+    return;
+  } else {
+    document.getElementById("result").innerHTML = convert(parsedInt, from, to);
+    // updateUI();
+  }
+}
+
+document.addEventListener("DOMContentLoaded", calculate);
\ No newline at end of file
diff --git a/styles.css b/styles.css
new file mode 100644 (file)
index 0000000..c841771
--- /dev/null
@@ -0,0 +1,41 @@
+:root {
+  --button: #0031a2;
+}
+*,
+::before,
+::after {
+  box-sizing: border-box;
+  margin: 0;
+  padding: 0;
+}
+html {
+  /* font-size: 62.5%; */
+}
+main {
+  width: 90%;
+  margin: 0 auto;
+}
+.converter-ctn {
+  box-shadow: 0rem 0.2rem 0.8rem #00000025;
+  padding: 1rem 2rem;
+  margin-top: 1.4rem;
+}
+.convertion-wrapper {
+  white-space: nowrap;
+}
+.selection-wrapper {
+  border: none;
+  display: block;
+  margin-top: 0.4rem;
+}
+select {
+  width: 50%;
+}
+input {
+  margin-top: 0.4rem;
+  width: 100%;
+}
+#result {
+  text-align: center;
+  padding: 1rem;
+}
\ No newline at end of file