forked from fineanmol/Hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 30
/
krishna2
78 lines (76 loc) · 2.2 KB
/
krishna2
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
//this file is for manipulation of dom
const searchForm=document.querySelector('.search-location');
const cityValue=document.querySelector('.search-location input');
const cityName=document.querySelector('.city-name p');
const cardbody=document.querySelector('.card-body');
const timeImage=document.querySelector('.card-top img');
const cardInfo=document.querySelector('.back-card');
const spitOutCelsius=(kelvin)=>
{
const celsius=Math.round(kelvin-273.15);
return celsius;
}
const isDayTime=(icon)=>{
if(icon.includes('d')){
return true;
}
else
{
return false;
}
}
updateWeatherApp=(city)=>{
console.log(city);
const iconSrc=`http://openweathermap.org/img/wn/${city.weather[0].icon}@2x.png`;
cityName.textContent=city.name;
cardbody.innerHTML= `
<div class="card-mid row">
<div class="col-8 text-center temp">
<span>${spitOutCelsius(city.main.temp)}°C</span>
</div>
<div class="col-4 condition-temp">
<p class="condition">${city.weather[0].description}</p>
<p class="high">${spitOutCelsius(city.main.temp_max)}°C</p>
<p class="low">${spitOutCelsius(city.main.temp_min)}°C</p>
</div>
</div>
<div class="icon-container card shadow mx-auto">
<img src="${iconSrc}" alt="" />
</div>
<div class="card-bottom px-5 py-4 row">
<div class="col text-center">
<p>${spitOutCelsius(city.main.feels_like)}°C</p>
<span>Feels Like</span>
</div>
<div class="col text-center">
<p>${city.main.humidity}%</p>
<span>Humidity</span>
</div>
</div>`
if(isDayTime(city.weather[0].icon))
{
timeImage.setAttribute('src','img/day_image.svg');
if (cityName.classList.contains('text-white')) {
cityName.classList.remove('text-white');
} else {
cityName.classList.add('text-black');
}
}
else
{
timeImage.setAttribute('src','img/nigh
}
//adding event listener to the form
searchForm.addEventListener('submit',(event)=>{
event.preventDefault();//prevents the page from refreshing on submitting
const citySearched=cityValue.value.trim();
console.log(citySearched);
searchForm.reset();
requestCity(citySearched)
.then((data)=>{
updateWeatherApp(data);
}).catch((error)=>{
console.log(error);
});
}
)