-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
115 lines (100 loc) · 4.52 KB
/
script.js
File metadata and controls
115 lines (100 loc) · 4.52 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Variables for UI elements
const button = document.getElementById("search-button");
const input = document.getElementById("city-input");
// Weather info elements
const cityNameElem = document.getElementById("city-loc");
const cityTimeElem = document.getElementById("city-time");
const cityTempElem = document.getElementById("city-temp");
const weatherConditionElem = document.getElementById("weather-condition");
const windSpeedElem = document.getElementById("wind-speed");
const humidityElem = document.getElementById("humidity");
const pressureElem = document.getElementById("pressure");
const uvIndexElem = document.getElementById("uv-index");
const feelsLikeElem = document.getElementById("feels-like");
const sunriseElem = document.getElementById("sunrise");
const sunsetElem = document.getElementById("sunset");
const weatherEmojiElem = document.getElementById("weather-emoji");
// UI sections for displaying weather data or error
const weatherInfo = document.getElementById("weather-info");
const errorMessage = document.getElementById("error-message");
// State variables for temperature unit (Celsius/Fahrenheit)
let tempUnit = 'C'; // default to Celsius
// Function to fetch weather data from the API
async function getWeatherData(city) {
const response = await fetch(
`https://api.weatherapi.com/v1/current.json?key=1316decc8a654820972162314251703&q=${city}&aqi=yes`
);
const data = await response.json();
return data;
}
// Function to convert Celsius to Fahrenheit
function celsiusToFahrenheit(celsius) {
return (celsius * 9 / 5) + 32;
}
// Function to update UI with fetched weather data
function updateWeatherData(data) {
cityNameElem.innerText = `${data.location.name}, ${data.location.region} - ${data.location.country}`;
cityTimeElem.innerText = `Local time: ${data.location.localtime}`;
let tempInCelsius = data.current.temp_c;
let feelsLikeInCelsius = data.current.feelslike_c;
// Display temperature in the selected unit (Celsius/Fahrenheit)
if (tempUnit === 'F') {
cityTempElem.innerText = `${celsiusToFahrenheit(tempInCelsius).toFixed(1)}°F`;
feelsLikeElem.innerText = `${celsiusToFahrenheit(feelsLikeInCelsius).toFixed(1)}°F`;
} else {
cityTempElem.innerText = `${tempInCelsius}°C`;
feelsLikeElem.innerText = `${feelsLikeInCelsius}°C`;
}
weatherConditionElem.innerText = data.current.condition.text;
windSpeedElem.innerText = `${data.current.wind_kph} km/h`;
humidityElem.innerText = `${data.current.humidity}%`;
pressureElem.innerText = `${data.current.pressure_mb} hPa`;
uvIndexElem.innerText = data.current.uv;
sunriseElem.innerText = data.forecast.forecastday[0].astro.sunrise;
sunsetElem.innerText = data.forecast.forecastday[0].astro.sunset;
const weatherCondition = data.current.condition.text.toLowerCase();
if (weatherCondition.includes('clear')) {
weatherEmojiElem.innerText = '☀️';
} else if (weatherCondition.includes('cloud')) {
weatherEmojiElem.innerText = '☁️';
} else if (weatherCondition.includes('rain')) {
weatherEmojiElem.innerText = '🌧️';
} else if (weatherCondition.includes('snow')) {
weatherEmojiElem.innerText = '❄️';
} else {
weatherEmojiElem.innerText = '🌈';
}
}
// Button click event handler for searching city weather
button.addEventListener("click", async () => {
const city = input.value.trim();
if (!city) {
errorMessage.classList.remove("hidden");
weatherInfo.classList.add("hidden");
return;
}
const result = await getWeatherData(city);
if (result.error) {
errorMessage.classList.remove("hidden");
weatherInfo.classList.add("hidden");
} else {
errorMessage.classList.add("hidden");
weatherInfo.classList.remove("hidden");
updateWeatherData(result);
}
});
// Button event listeners for Celsius/Fahrenheit toggling
document.getElementById("celsius-btn").addEventListener("click", () => {
tempUnit = 'C';
const currentCity = cityNameElem.innerText.split(",")[0].trim();
if (currentCity) {
getWeatherData(currentCity).then(updateWeatherData);
}
});
document.getElementById("fahrenheit-btn").addEventListener("click", () => {
tempUnit = 'F';
const currentCity = cityNameElem.innerText.split(",")[0].trim();
if (currentCity) {
getWeatherData(currentCity).then(updateWeatherData);
}
});