-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforecast.js
58 lines (46 loc) · 1.79 KB
/
forecast.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
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
import {iconImage} from './weather.js';
const searchInput = document.querySelector('.js-search');
export async function updateForecast(){
const city = searchInput.value;
if(city){
try{
const data = await getForecast(city);
console.log(data);
let forecastData = '';
for(let i=0 ; i< 40 ; i=i+8){
const dayData = data.list[i];
//* 1000 because to convert it in millisecond
const date = new Date(dayData.dt * 1000);
let day;
if(i === 0){
day = 'Today';
}
else{
day = date.toLocaleDateString('en-US', { weekday: 'short' });
}
const temperature = Math.round(dayData.main.temp);
const icon = iconImage(dayData);
forecastData += `
<div>
<p class="forecast-day">${day}</p>
<img src="${icon}" class="forecast-icon js-forecast-icon">
<p class="forecast-temperature">${temperature}°C</p>
</div>
`;
}
document.querySelector('.js-forecast-info').innerHTML = forecastData;
}catch(error){
console.error('Error fetching weather data:', error);
}
}
}
async function getForecast(city){
const apiKey = "807b0a1f0c5726475a4225e8eb35a177";
const apiUrl = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${apiKey}&units=metric`;
const response = await fetch(apiUrl);
if(!response.ok){
throw new Error(`HTTP error! Status: ${response.status}`)
}
const data = await response.json();
return data;
}