-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava.js
101 lines (90 loc) · 3.82 KB
/
java.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const app=document.querySelector('.weather-app');
const temp=document.querySelector('.temp');
const dateOutput=document.querySelector('.date');
const timeOutput=document.querySelector('.time');
const conditionOutput=document.querySelector('.condition');
const nameOutput=document.querySelector('.name');
const icon=document.querySelector('.icon');
const cloudOutput=document.querySelector('.cloud');
const humidityOutput=document.querySelector('.humidity');
const windOutput=document.querySelector('.wind');
const form=document.getElementById('.locationInput');
const search=document.querySelector('.search');
const btn=document.querySelector('.submit');
const cities=document.querySelectorAll('.city');
let cityInput="Lucknow";
cities.forEach((city)=>{
city.addEventListener('click',(e)=>{
cityInput=e.target.innerHTML;
fetchWeatherData(cityInput);
app.style.opacity="0";
});
});
btn.addEventListener('click' || 'submit', (e) => {
e.preventDefault();
if (search.value.length == 0) {
alert('Please enter the city name');
} else {
cityInput = search.value;
// console.log(cityInput);
app.style.opacity = "0";
fetchWeatherData(cityInput);
search.value = "";
}
});
function dayOfTheWeek(day, month, year) {
const weekday = [
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
];
const date = new Date(`${month}/${day}/${year}`);
const dayOfWeek = weekday[date.getDay()];
return dayOfWeek;
}
function fetchWeatherData(cityInput) {
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${cityInput}&appid=94dd891924a718733889af7b72b92657`)
.then(response => response.json())
.then(data => {
let a=`${data.main.temp}`;
a=Math.floor(a-273.15);
temp.innerHTML=`${a}°`;
conditionOutput.innerHTML = data.weather[0].description;
const date = new Date(data.dt * 1000);
const y = date.getFullYear();
const m = date.getMonth() + 1;
const d = date.getDate();
const time = date.toLocaleTimeString();
dateOutput.innerHTML = `${dayOfTheWeek(d, m, y)} ${d}, ${m} ${y}`;
timeOutput.innerHTML = time;
nameOutput.innerHTML = data.name;
const iconId = data.weather[0].icon;
icon.src = `http://openweathermap.org/img/w/${iconId}.png`;
cloudOutput.innerHTML = `${data.clouds.all}%`;
humidityOutput.innerHTML = `${data.main.humidity}%`;
windOutput.innerHTML = `${data.wind.speed} m/s`;
let timeOfDay = "day";
if (data.dt < data.sys.sunrise || data.dt > data.sys.sunset) {
timeOfDay = "night";
}
const weatherCode = data.weather[0].id;
if (weatherCode >= 200 && weatherCode <= 232) {
app.style.backgroundImage = `url(./images/${timeOfDay}/thunderstorm.jpg)`;
} else if (weatherCode >= 300 && weatherCode <= 321) {
app.style.backgroundImage = `url(./images/${timeOfDay}/drizzle.jpg)`;
} else if (weatherCode >= 500 && weatherCode <= 531) {
app.style.backgroundImage = `url(./images/${timeOfDay}/rain.jpg)`;
} else if (weatherCode >= 600 && weatherCode <= 622) {
app.style.backgroundImage = `url(./images/${timeOfDay}/snow.jpg)`;
} else if (weatherCode >= 701 && weatherCode <= 781) {
app.style.backgroundImage = `url(./images/${timeOfDay}/mist.jpg)`;
} else if (weatherCode === 800) {
app.style.backgroundImage = `url(./images/${timeOfDay}/clear.jpg)`;
} else if (weatherCode >= 801 && weatherCode <= 804) {
app.style.backgroundImage = `url(./images/${timeOfDay}/clouds.jpg)`;
}
app.style.opacity = "1";
})
.catch(() => {
alert("This city doesn't exist");
app.style.opacity = "1";
});
}