-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
72 lines (56 loc) · 2.36 KB
/
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
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
'use strict'
//Api çağır
const api = {
key: "38a9abd05e9b566ea33a8f32aacb654a",
base: "https://api.openweathermap.org/data/2.5/"
}
//Değişkenler
const search = document.querySelector(".search");
const btn = document.querySelector(".btn");
btn.addEventListener('click', getInput);
function getInput(event){
event.preventDefault();
if(event.type == "click"){
getData(search.value);
console.log(search.value)
}
}
function getData(){
//openweathermap document'inden https://openweathermap.org/api/one-call-3
fetch(`${api.base}weather?q=${search.value}&units=metric&appid=${api.key}`).then(response =>{
return response.json();
}).then(displayData);
}
function displayData(response){
// console.log(response);
if(response.cod === "404"){
const error = document.querySelector('.error');
error.textContent = "Please enter a valid citr";
search.value = "";
} else{
const city = document.querySelector(".city");
city.innerText = `${response.name}, ${response.sys.country}`;
const today = new Date();
const date = document.querySelector(".date");
date.innerText = dateFunction(today);
const temp = document.querySelector(".temp");
temp.innerHTML = `Temp: ${Math.round(response.main.temp, temp)} <span>°C</span>`;
const weather = document.querySelector(".weather");
weather.innerText = `Weather: ${response.weather[0].main}`;
const tempRange = document.querySelector(".temp-range");
tempRange.innerText = `Temp Range: ${Math.round(response.main.temp_min)}°C / ${Math.round(response.main.temp_max)}°C`;
const weatherIcon = document.querySelector(".weather-icon");
const iconURL = "https://api.openweathermap.org/img/w/";
weatherIcon.src = iconURL + response.weather[0].icon + ".png";
search.value = "";
}
}
function dateFunction(d){
let months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"];
let days = ["Sunday", "Monday", "Thuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
let day = days[d.getDay()];
let date = d.getDate();
let month = months[d.getMonth()];
let year = d.getFullYear();
return `${day}, ${date}, ${month}, ${year}`;
}