-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
37 lines (31 loc) · 1.36 KB
/
main.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
//selector variable
var inputval = document.querySelector('#cityinput')
var btn = document.querySelector('#add');
var city = document.querySelector('#cityoutput')
var descrip = document.querySelector('#description')
var temp = document.querySelector('#temp')
var wind = document.querySelector('#wind')
// Get your own free OWM API key at https://www.openweathermap.org/appid - please do not re-use mine!
// You don't need an API key for this to work at the moment, but this will change eventually.
apik = "3045dd712ffe6e702e3245525ac7fa38"
//kelvin to celcious
function convertion(val) {
return (val - 273).toFixed(2)
}
//fetch
btn.addEventListener('click', function () {
fetch('https://api.openweathermap.org/data/2.5/weather?q=' + inputval.value + '&appid=' + apik)
.then(res => res.json())
//.then(data => console.log(data))
.then(data => {
var nameval = data['name']
var descrip = data['weather']['0']['description']
var tempature = data['main']['temp']
var wndspd = data['wind']['speed']
city.innerHTML = `City: ${nameval}`
temp.innerHTML = `Temperature: ${convertion(tempature)} C`
description.innerHTML = `Conditions: ${descrip}`
wind.innerHTML = `Wind Speed: ${wndspd} km/h`
})
.catch(err => alert('You entered Wrong city name'))
})