-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweathergenie.html
67 lines (61 loc) · 2.26 KB
/
weathergenie.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WeatherGenie</title>
<style>
/* Add your custom CSS styles here */
#middleImage {
display: block;
margin: 0 auto;
<button onclick="window.location.href = 'index.html';">Back to ChatGenie</button>
</style>
</head>
<body style="background-color:#7B1FA2;">
<h1>WeatherGenie</h1>
<div>
<label for="city">Enter City:</label>
<input type="text" id="city" placeholder="Enter city name">
<button onclick="fetchWeather()">Get Weather</button>
</div>
<div id="weatherResult">
<!-- Weather data will be displayed here -->
</div>
<!-- Image added here -->
<script>
function fetchWeather() {
const apiKey = '681f558146ec408aa24203103241302';
const city = document.getElementById('city').value;
const apiUrl = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${city}`;
fetch(apiUrl)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
displayWeather(data);
})
.catch(error => {
console.error('There was a problem with your fetch operation:', error);
});
}
function displayWeather(data) {
const weatherResult = document.getElementById('weatherResult');
const weather = data.current;
const cityName = data.location.name;
const countryName = data.location.country;
const temperatureCelsius = weather.temp_c;
const temperatureFahrenheit = weather.temp_f;
const condition = weather.condition.text;
weatherResult.innerHTML = `
<h2>${cityName}, ${countryName}</h2>
<p>Temperature: ${temperatureFahrenheit}°F (${temperatureCelsius}°C)</p>
<p>Condition: ${condition}</p>
`;
}
</script>
</body>
</html>