-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
34 lines (26 loc) · 1023 Bytes
/
script.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
const url = 'https://api.openweathermap.org/data/2.5/'
const key = '66b99d9572736d533c04cf5740c9c545'
const setQuery = (e) => {
if(e.keyCode =='13')
getResult(searchBar.value)
}
const getResult = (cityName) => {
let query = `${url}weather?q=${cityName}&appid=${key}&units=metric&lang=tr`
fetch(query)
.then(weather => {
return weather.json()
})
.then(displayResult)
}
const displayResult = (result) => {
let city = document.querySelector('.city')
city.innerText = `${result.name}, ${result.sys.country}`
let temp = document.querySelector('.temp')
temp.innerText = `${Math.round(result.main.temp)}°C`
let desc = document.querySelector('.desc')
desc.innerText = result.weather[0].description
let minmax = document.querySelector('.minmax')
minmax.innerText = `${Math.round(result.main.temp_min)}°c / ${Math.round(result.main.temp_max)}°c`
}
const searchBar = document.getElementById('searchBar')
searchBar.addEventListener('keypress',setQuery)