-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
54 lines (33 loc) · 1.57 KB
/
app.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
const api = {
key: "4930558afcaeeec187450879e0493743",
base: "https://api.openweathermap.org/data/2.5/"
}
const searchBox = document.querySelector(".search-box");
const searchCity = document.querySelector(".city");
const searchDate = document.querySelector(".date");
const searchTemp = document.querySelector(".temp");
const searchWeather = document.querySelector(".weather");
const searchHiLow = document.querySelector(".hi-low");
const addCard = document.querySelector(".hidden");
function getQuery(e){
if(e.keyCode == 13){
fetch(`${api.base}weather?q=`+searchBox.value+`&units=metric&appid=${api.key}`).then(response => response.json()).then(dataDisplay)
; //fetching data (metric celcius)
}
}
function dataDisplay(response){
if(response.cod === "404"){
addCard.classList.add('card');
searchCity.innerText = 'Invalid City'; //1st way
searchTemp.innerText = 'N/A';
searchWeather.innerText = 'N/A'; //2nd way
searchHiLow.innerText = 'N/A' + '/'+ 'N/A';
}else{
addCard.classList.add('card');
searchCity.innerText = response['name'] + ','+ response['sys']['country']; //1st way
searchTemp.innerText = response['main']['temp'] + String.fromCharCode(176) + 'c';
searchWeather.innerText = response.weather[0].main; //2nd way
searchHiLow.innerText = response.main.temp_max + String.fromCharCode(176) + 'c'+ '/'+ response.main.temp_min + String.fromCharCode(176)+ 'c'
}
}
searchBox.addEventListener('keypress', getQuery);