-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (64 loc) · 3.02 KB
/
script.js
File metadata and controls
77 lines (64 loc) · 3.02 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
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
// Open Weather API Key and URL
const apiKey = "b25a2f499d7c94ef57073bd237f31b16";
const apiUrl = "https://api.openweathermap.org/data/2.5/weather?units=metric&q=";
// Some Variables for Searching and Weather Icons
const searchBox = document.querySelector(".search input");
const searchBtn = document.querySelector(".search button");
const weatherIcon = document.querySelector(".weather-icon");
// Async Function to Give the Weather Data fetch from API.
async function checkWeather(city){
const response = await fetch(apiUrl + city + `&appid=${apiKey}`);
if(response.status == 404){
//Error Message
document.querySelector(".error").style.display = "Block";
document.querySelector(".weather").style.display = "none";
}else{
//stores the APIs data in variable
var data = await response.json();
//Printing the Data on Console For Easy Handling and Understanding
console.log(data);
//To calculate the Day and Date From the APIs Timezone
let DayDate = new Date(data.dt*1000-(data.timezone*1000));
let date = DayDate.getDate();
let month = DayDate.toLocaleString('default', {month: 'short'});
//Using DOM Selector for displaying data
document.querySelector(".date").innerHTML = month.toUpperCase() + " " + date;
document.querySelector(".weather-status").innerHTML = data.weather[0].main
document.querySelector(".city").innerHTML = data.name;
document.querySelector(".temp").innerHTML =Math.round(data.main.temp) + "°c";
document.querySelector(".humidity").innerHTML = data.main.humidity + "%";
document.querySelector(".wind").innerHTML = data.wind.speed + " km/h";
//Conditions for the Weather Status Icon
if(data.weather[0].main == "Clouds"){
weatherIcon.src = "Images/clouds.png";
}else if(data.weather[0].main == "Clear"){
weatherIcon.src = "Images/clear.png";
}else if(data.weather[0].main == "Rain"){
weatherIcon.src = "Images/rain.png";
}else if(data.weather[0].main == "Drizzle"){
weatherIcon.src = "Images/drizzle.png";
}else if(data.weather[0].main == "Mist"){
weatherIcon.src = "Images/mist.png";
}else if(data.weather[0].main == "Thunderstorm"){
weatherIcon.src = "Images/thunderstorm.png";
}else if(data.weather[0].main == "Haze"){
weatherIcon.src = "Images/haze.png";
}else if(data.weather[0].main == "Snow"){
weatherIcon.src = "Images/snow.png";
}
//Hides the Error Block from HTML And Display The Data
document.querySelector(".weather").style.display = 'Block';
document.querySelector(".error").style.display = "none";
}
}
// Two Event Listeners
//1. On CLICK of Search Icon
searchBtn.addEventListener("click", ()=>{
checkWeather(searchBox.value);
});
// 2. On Pressing Enter
searchBox.addEventListener("keypress", function (e){
if(e.key === 'Enter'){
checkWeather(searchBox.value);
}
});