浏览代码

feat: Initial commit

Douglas A 2 年之前
当前提交
608365f9a6
共有 3 个文件被更改,包括 68 次插入0 次删除
  1. 7 0
      index.css
  2. 15 0
      index.html
  3. 46 0
      index.js

+ 7 - 0
index.css

@@ -0,0 +1,7 @@
+body {
+    font-family: Hack, Verdana;
+};
+
+p1 {
+    color: #000000;
+}

+ 15 - 0
index.html

@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta charset="UTF-8"> 
+        <link rel="stylesheet" href="index.css">
+        <title>Current Temperature</title>
+    </head>
+<body>
+    <h1>Hello World</h1>
+    <p>Current temperature: <div id="city"></div> <div id="temp"></div></p>
+
+    <script src="index.js"></script>
+</body>
+
+</html>

+ 46 - 0
index.js

@@ -0,0 +1,46 @@
+const API_KEY = "3516a881cfe62a50e7d405757def3a1a";
+
+async function get_temperature(city) {
+    let parsed_city = encodeURIComponent(city);
+    const url = `https://api.openweathermap.org/data/2.5/weather?q=${parsed_city}&appid=${API_KEY}`
+
+    let get_result = fetch(url).then((result) => {
+        let data = result.json().then((json_data) => {
+            return json_data['main']['temp'];
+        });
+
+        return data;
+
+    });
+
+    return get_result;
+}
+
+async function main() {
+    const city = "Poços de Caldas";
+    const temp = await get_temperature(city);
+
+    let divCity = document.querySelector("#city");
+    if (!divCity) { 
+        console.log("Could not find city selector in document");
+        return;
+
+    }
+    let divTemp = document.querySelector("#temp");
+    if (!divTemp) {
+        console.log("Could not find temp selector in document");
+        return;
+    }
+
+    if (!temp) {
+        console.log("Could not resolve temperature");
+        return;
+    }
+    divCity.textContent = city;
+
+    divTemp.textContent = (temp - 273.00).toFixed(2).toString() + " C";
+}
+
+
+
+main();