-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
67 lines (63 loc) · 2.55 KB
/
script.js
File metadata and controls
67 lines (63 loc) · 2.55 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
let weather = {
apiKey:'f81584a7e55b2e65c19748fc62248e48', // i went to open weather.org and then made my account after doing these things i just
// went to the option of API Key and fetch the Key from that place.
fetchWeather: function (city) {
fetch(
'https://api.openweathermap.org/data/2.5/weather?q=' +
city +
'&units=metric&appid=' +
this.apiKey
)
.then(response => response.json())
.then(data => this.displayWeather(data));
},
displayWeather: function (data) {
const { name } = data;
const { icon, description } = data.weather[0];
const { temp, humidity } = data.main;
const { speed } = data.wind;
const { sunrise, sunset } = data.sys;
function whenSunset(sunset) {
/// these are the function that converted UTC to local date formate
let sunSet = sunset * 1000;
const dateObject = new Date(sunSet);
const humanDateFormat = dateObject.toLocaleString();
return humanDateFormat;
}
function whenSunrise(sunrise) {
let sunRise = sunrise * 1000;
const dateObject = new Date(sunRise);
const humanDateFormat = dateObject.toLocaleString();
return humanDateFormat;
}
document.querySelector('.city').innerText = name;
document.querySelector('.temp').innerText = temp.toFixed(1) + '°C'; // here toFixed is used to retrict the temp to one decimals
document.querySelector('.description').innerText =
description.toUpperCase();
document.querySelector('.icon').src =
'https://openweathermap.org/img/wn/' + icon + '@2x.png';
document.querySelector('.wind').innerText =
'Wind Speed: ' + (speed * 3.6).toFixed(1) + ' km/h'; // here i converted m/s to km/h by multiplying it by 3.6
document.querySelector('.humidity').innerText =
'Humidity: ' + humidity + '%';
document.querySelector('.sunset').innerText =
'Sunset : ' + whenSunset(sunset); // here i made 2 function that converted UTC to normal date formate
document.querySelector('.sunrise').innerText =
'Sunrise : ' + whenSunrise(sunrise);
document.body.style.backgroundImage = "url('https://source.unsplash.com/1600x900/?" + name + "' )"
},
search: function () {
this.fetchWeather(document.querySelector('.search-bar').value);
}
};
document.querySelector('.search button').addEventListener('click', function () {
weather.search();
});
document
.querySelector('.search-bar')
.addEventListener('keyup', function (event) {
if (event.key === 'Enter') {
weather.search();
}
});
weather.fetchWeather('Patna');