-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.js
25 lines (22 loc) · 946 Bytes
/
weather.js
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
// weather.js
const apiKey = '2248cd52189f2e12c93c5f17a33cf2f51'; // Updated API Key
const city = 'Greenfield,IN';
const url = `https://api.openweathermap.org/data/2.5/weather?id=4258285&appid=248cd52189f2e12c93c5f17a33cf2f51&units=imperial`;
async function getWeather() {
try {
const response = await fetch(url);
if (!response.ok) throw new Error('Network response was not ok ' + response.statusText);
const weatherData = await response.json();
displayWeather(weatherData);
} catch (error) {
console.error('There has been a problem with your fetch operation:', error);
}
}
function displayWeather(data) {
const temp = data.main.temp;
const conditions = data.weather[0].description;
document.getElementById('temperature').innerText = `Temperature: ${temp}°F`;
document.getElementById('conditions').innerText = `Conditions: ${conditions}`;
}
// Call the function to get and display the weather
getWeather();