-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
40 lines (37 loc) · 1.13 KB
/
script.js
File metadata and controls
40 lines (37 loc) · 1.13 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
const url =
'https://api.openweathermap.org/data/2.5/weather';
const apiKey =
'f00c38e0279b7bc85480c3fe775d518c';
$(document).ready(function () {
weatherFn('Noida'); // Set Noida as the initial city
});
async function weatherFn(cName) {
const temp =
`${url}?q=${cName}&appid=${apiKey}&units=metric`;
try {
const res = await fetch(temp);
const data = await res.json();
if (res.ok) {
weatherShowFn(data);
} else {
alert('City not found. Please try again.');
}
} catch (error) {
console.error('Error fetching weather data:', error);
}
}
function weatherShowFn(data) {
$('#city-name').text(data.name);
$('#date').text(moment().
format('MMMM Do YYYY, h:mm:ss a')); // Corrected date format to include year
$('#temperature').
html(`${Math.round(data.main.temp)}°C`); // Rounded temperature
$('#description').
text(data.weather[0].description);
$('#wind-speed').
html(`Wind Speed: ${data.wind.speed} m/s`);
$('#weather-icon').
attr('src',
`http://openweathermap.org/img/wn/$%7Bdata.weather%5B0%5D.icon%7D@2x.png%60); // Corrected icon URL
$('#weather-info').fadeIn();
}