-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
84 lines (67 loc) · 2.21 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
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
// calling all elements from document
const weatherElement = document.getElementById("weather");
const celBtn = document.getElementById("celBtn");
const fahrBtn = document.getElementById("fahrBtn");
const emoji = document.getElementById("emoji");
const time = document.getElementById("time");
//API URL parameters: London 1 day °C is_day
const url = "https://api.open-meteo.com/v1/forecast?latitude=51.5085&longitude=-0.1257¤t=temperature_2m,is_day&forecast_days=1";
// Main function to retrieve and display current weather
async function getAndDisplayWeather() {
const weatherObject = await retrieveWeather();
displayWeather(weatherObject);
dayOrNight(weatherObject);
timeDisplay();
}
// Function to retrieve weather
async function retrieveWeather() {
const response = await fetch(url,{
headers:{
Accept: "application/json"
}
});
console.log(response);
if(!response.ok){
console.error(response.status);
console.error(response.text());
}
const data = await response.json();
// console.log(data.weather);
console.log(data);
return data;
};
// Function to update the DOM with the current weather
function displayWeather(weather) {
const currentTemp = weather.current.temperature_2m;
weatherElement.textContent = currentTemp + "°C";
return currentTemp;
};
// day function
function dayOrNight(weather) {
const isDay = weather.current.is_day;
console.log(isDay)
if (isDay) {
emoji.src = "cloud.png";
} else {
emoji.src = "night.png";
};
};
// time display function
function timeDisplay() {
const now = new Date();
const hours = now.getHours();
const minutes = now.getMinutes();
time.textContent = `${hours}:${minutes}`;
};
// conversion function
async function toFahrenheit() {
const weatherObject = await retrieveWeather();
const tempF = Math.ceil((displayWeather(weatherObject)* 1.8) + 32);
weatherElement.textContent = tempF +"°F";
};
// Event listeners
// Waits for the DOM to be fully loaded and then displays current weather.
document.addEventListener("DOMContentLoaded", getAndDisplayWeather);
// Sets up a click event listener for buttons
celBtn.addEventListener("click", getAndDisplayWeather);
fahrBtn.addEventListener("click", toFahrenheit);