-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
85 lines (71 loc) · 2.7 KB
/
script.js
File metadata and controls
85 lines (71 loc) · 2.7 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
78
79
80
81
82
83
84
85
const btn = document.getElementById('search-icon');
btn.addEventListener('click', function(){
if(document.getElementById('searchbar').value ===''){
alert('Error: Please enter name of a city');
}
else{
let cityname = document.getElementById('searchbar').value;
getWeather(cityname)
.then( function(response){
return response;
})
.then(function newResponse(response){
loadData(response);
})
.catch(function(err){
console.log('Request failed! Error ' + err);
});
}
});
// function getWeather(city){
// return new Promise(function(resolve, reject){
// const apiURL = `http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=bf1bedbf24ebcc81a53dcd314db85f38`;
// let xhr = new XMLHttpRequest();
// xhr.open('GET', apiURL);
// xhr.onload = function(){
// if(this.status==200){
// resolve(JSON.parse(this.responseText));
// }
// else{
// reject(this.status);
// }
// }
// xhr.send();
// });
// };
async function getWeather(city){
const apiURL = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=bf1bedbf24ebcc81a53dcd314db85f38`;
const response = await fetch(apiURL);
const data = await response.json();
return(data);
}
/*show the answer-container*/
function loadData(data){
document.getElementById('hi').classList.add('answer-container');
document.getElementById('a1').innerHTML = data.name;
document.getElementById('a1').style.textTransform = 'capitalize';
document.getElementById('a2').innerHTML = checkLatitude(data.coord.lat) + ' & ' + checkLongitude(data.coord.lon);
document.getElementById('a3').innerHTML = data.weather[0].main ;
document.getElementById('a4').innerHTML = kelToCelcius(data.main.temp);
document.getElementById('a5').innerHTML = kelToCelcius(data.main.feels_like);
document.getElementById('a7').innerHTML = kelToCelcius(data.main.temp_max);
document.getElementById('a6').innerHTML = kelToCelcius(data.main.temp_min);
document.getElementById('a8').innerHTML = data.wind.speed+ ' m/s';
document.getElementById('a9').innerHTML = data.clouds.all + ' %';
document.getElementById('a10').innerHTML = data.main.humidity + ' %';
};
function kelToCelcius(kelvinTemp){
return ((kelvinTemp-273.15).toFixed(2)+' C');
}
function checkLongitude(lon){
if(lon >= 0){
return lon+' E';
}
return lon*(-1) + ' W';
};
function checkLatitude(lat){
if(lat >= 0){
return lat + ' N';
}
return lat*(-1) + ' S';
};