-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
53 lines (49 loc) · 1.99 KB
/
script.js
File metadata and controls
53 lines (49 loc) · 1.99 KB
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
const searchButton = document.getElementById('search');
const cityInput = document.getElementById('city');
const cityName = document.getElementById('city-name');
const weatherInfo = document.getElementById('weather-info');
const windInfo = document.getElementById('wind-info');
const precipitationInfo = document.getElementById('precipitation-info');
const temperatureInfo = document.getElementById('temperature-info');
searchButton.addEventListener('click', () => {
const city = cityInput.value;
if (city) {
fetchWeather(city);
fetchWeather1(city);
}
});
function fetchWeather(city) {
const apiUrl = `https://wttr.in/${city}?format=%C+%t`;
fetch(apiUrl)
.then(response => response.text())
.then(data => {
// Use regular expressions to match condition and temperature
const matches = data.match(/^(.*?)\s+([-+]?\d+(\.\d+)?)/);
if (matches && matches.length >= 3) {
const condition = matches[1];
const temperature = matches[2];
cityName.textContent = city;
weatherInfo.textContent = `Condition: ${condition} `;
temperatureInfo.textContent = `Temperature: ${temperature}°C`;
} else {
cityName.textContent = 'Invalid Data';
weatherInfo.textContent = '';
}
})
.catch(error => {
console.error('Error fetching weather data:', error);
});
}
function fetchWeather1(city) {
const apiUrl = `https://wttr.in/${city}?format=%w+%m`;
fetch(apiUrl)
.then(response => response.text())
.then(data => {
const [wind, precipitation] = data.split(' ');
windInfo.textContent = `Wind: ${wind}`;
precipitationInfo.textContent=`Precipitation: ${precipitation}`;
})
.catch(error => {
console.error('Error fetching weather data:', error);
});
}