File size: 2,668 Bytes
524bbfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Weather App</title>
  <style>

    body {

      font-family: Arial, sans-serif;

      background: #87CEEB;

      display: flex;

      justify-content: center;

      align-items: center;

      height: 100vh;

      margin: 0;

    }

    .weather-container {

      background: white;

      padding: 20px 30px;

      border-radius: 10px;

      box-shadow: 0 0 15px rgba(0,0,0,0.2);

      text-align: center;

      width: 300px;

    }

    input[type="text"] {

      width: 80%;

      padding: 10px;

      font-size: 16px;

      border-radius: 5px;

      border: 1px solid #ccc;

      margin-bottom: 15px;

    }

    button {

      padding: 10px 20px;

      font-size: 16px;

      background: #007BFF;

      color: white;

      border: none;

      border-radius: 5px;

      cursor: pointer;

    }

    button:hover {

      background: #0056b3;

    }

    .result {

      margin-top: 20px;

      font-size: 20px;

    }

  </style>
</head>
<body>
  <div class="weather-container">
    <h2>Weather App</h2>
    <input type="text" id="location" placeholder="Enter Location (City or Zip)" />
    <button onclick="getWeather()">Get Temperature</button>
    <div class="result" id="result"></div>
  </div>

  <script>

    async function getWeather() {

      const location = document.getElementById('location').value;

      const resultDiv = document.getElementById('result');

      if (!location) {

        resultDiv.textContent = 'Please enter a location.';

        return;

      }



      const apiKey = '4b0669fa25fd4d5292b93938251311';

      const url = `http://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${encodeURIComponent(location)}&aqi=yes`;



      resultDiv.textContent = 'Loading...';



      try {

        const response = await fetch(url);

        if (!response.ok) {

          throw new Error('Location not found');

        }

        const data = await response.json();



        const tempC = data.current.temp_c;

        const condition = data.current.condition.text;



        resultDiv.innerHTML = `

          <p><strong>Location:</strong> ${data.location.name}, ${data.location.region}, ${data.location.country}</p>

          <p><strong>Temperature:</strong> ${tempC} °C</p>

          <p><strong>Condition:</strong> ${condition}</p>

        `;

      } catch (error) {

        resultDiv.textContent = 'Could not retrieve weather data. Please check the location and try again.';

      }

    }

  </script>
</body>
</html>