-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
491 lines (435 loc) · 15.8 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
document.addEventListener("DOMContentLoaded", function () {
let header = document.getElementById("header");
let cityInput = document.getElementById("city");
let searchBtn = document.getElementById("search");
let tempC = document.getElementById("temp-c");
let tempF = document.getElementById("temp-f");
let windKph = document.getElementById("wind-kph");
let windMph = document.getElementById("wind-mph");
let windStatus = document.getElementById("wind-status");
let conditionDiv = document.querySelector(".condition-div");
let locationHeading = document.querySelector(".location");
let body = document.querySelector("body");
let timeZone = document.querySelector(".time-zone");
let hourFormat = document.querySelector(".hour-format");
let nextTwoDaysDiv = document.querySelector(".next-two-days");
let dateForecastDiv = document.querySelector(".date-forecast");
let todayForecast = document.createElement("p");
let tomorrowForecast = document.createElement("p");
let afterTomorrowForecast = document.createElement("p");
let forecastDivMain = document.querySelector(".forecast-div");
let rainChance = document.querySelector(".rain-chance");
let darkModeBtn = document.getElementById("darkMode");
let container = document.querySelector(".container");
let todayReading;
let tomorrowReading;
let afterTomorrowReading;
let WEATHER;
let snow;
let todaySnowChance;
let tomorrowSnowChance;
let afterTomorrowSnowChance;
let timeCheck;
async function fetchWeatherData(city) {
if (!city) {
alert("Enter a city or country.");
return;
}
const url = `https://weatherapi-com.p.rapidapi.com/current.json?q=${encodeURIComponent(
city
)}`;
const options = {
method: "GET",
headers: {
"x-rapidapi-key": "73736933c8msh0e36fc0e81a021cp1634e9jsn68c86f8cf32e",
"x-rapidapi-host": "weatherapi-com.p.rapidapi.com",
},
};
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error("Network response not okay.");
}
const result = await response.json();
if (result.error) {
alert(result.error.message);
return;
}
updateLocation(result.location.name, result.location.country);
updateTemperature(result.current.temp_c, result.current.temp_f);
windSpeed(result.current.wind_kph, result.current.wind_mph);
timeZoneArea(result.location.tz_id);
updateConditionAndIcon(
result.current.condition.text,
result.current.condition.icon
);
} catch (error) {
console.error(error.message);
alert("Please enter a city or country!");
locationHeading.textContent = `Please enter city or country.`;
}
}
async function fetchForecast(city) {
const url = `https://weatherapi-com.p.rapidapi.com/forecast.json?q=${encodeURIComponent(
city
)}&days=5`;
const options = {
method: "GET",
headers: {
"x-rapidapi-key": "73736933c8msh0e36fc0e81a021cp1634e9jsn68c86f8cf32e",
"x-rapidapi-host": "weatherapi-com.p.rapidapi.com",
},
};
try {
const response = await fetch(url, options);
const result = await response.json();
console.log(result);
rainChanceBorder();
dateCast(
result.forecast.forecastday[0].date,
result.forecast.forecastday[1].date,
result.forecast.forecastday[2].date
);
nextTwoDaysForecast(
result.forecast.forecastday[0].day.daily_chance_of_rain,
result.forecast.forecastday[1].day.daily_chance_of_rain,
result.forecast.forecastday[2].day.daily_chance_of_rain
);
snowChance(
result.forecast.forecastday[0].day.daily_will_it_snow,
result.forecast.forecastday[1].day.daily_will_it_snow,
result.forecast.forecastday[2].day.daily_will_it_snow
);
forecastIcons(
result.forecast.forecastday[0].day.daily_will_it_rain,
result.forecast.forecastday[1].day.daily_will_it_rain,
result.forecast.forecastday[2].day.daily_will_it_rain
);
} catch (error) {
console.error(error);
}
}
function updateConditionAndIcon(text, icon) {
conditionDiv.innerHTML = "";
const ICON_IMG = document.createElement("img");
const TEXT_CONDITION = document.createElement("p");
ICON_IMG.classList.add("icon-img");
ICON_IMG.src = icon;
ICON_IMG.alt = icon;
TEXT_CONDITION.textContent = `${text}`;
conditionDiv.appendChild(TEXT_CONDITION);
conditionDiv.appendChild(ICON_IMG);
WEATHER = text.toLowerCase();
const HOUR = parseInt(timeCheck, 10);
switch (true) {
case WEATHER.includes("clear") && (HOUR >= 18 || HOUR < 6):
body.style.backgroundImage = "url('backgrounds/nightClear.jpg')";
break;
case (WEATHER.includes("cloud") || WEATHER.includes("overcast")) &&
(HOUR >= 18 || HOUR < 6):
body.style.backgroundImage = "url('backgrounds/nightCloudy.jpg')";
break;
case WEATHER.includes("snow") && (HOUR >= 18 || HOUR < 6):
body.style.backgroundImage = "url('backgrounds/nightCold.jpg')";
break;
case WEATHER.includes("sunny") && (HOUR >= 18 || HOUR < 6):
body.style.backgroundImage = "url('backgrounds/nightSunny.jpg')";
break;
case WEATHER.includes("rain"):
body.style.backgroundImage = "url('backgrounds/rainy.jpg')";
break;
case WEATHER.includes("sunny"):
body.style.backgroundImage = "url('backgrounds/sunny.jpg')";
break;
case WEATHER.includes("clear"):
body.style.backgroundImage = "url('backgrounds/clear.jpg')";
break;
case WEATHER.includes("snow"):
body.style.backgroundImage = "url('backgrounds/snow.jpg')";
break;
case WEATHER.includes("cloud") || WEATHER.includes("overcast"):
body.style.backgroundImage = "url('backgrounds/cloudy.jpg')";
break;
default:
body.style.backgroundImage = "url('backgrounds/backgroundImg.jpg')";
break;
}
}
function snowChance(snowToday, snowTomorrow, snowAfterTomorrow) {
todaySnowChance = snowToday;
tomorrowSnowChance = snowTomorrow;
afterTomorrowSnowChance = snowAfterTomorrow;
}
function dateCast(today, tomorrow, afterTomorrow) {
const ST = [1, 21, 31];
const ND = [2, 22];
const RD = [3, 23];
dateForecastDiv.innerHTML = "";
let todayDate = document.createElement("p");
let tomorrowDate = document.createElement("p");
let afterTomorrowDate = document.createElement("p");
today = parseInt(today.slice(8, 10));
tomorrow = parseInt(tomorrow.slice(8, 10));
afterTomorrow = parseInt(afterTomorrow.slice(8, 10));
console.log(today);
console.log(typeof today);
if (ST.includes(today)) {
todayDate.textContent = `${today}st`;
} else if (ND.includes(today)) {
todayDate.textContent = `${today}nd`;
} else if (RD.includes(today)) {
todayDate.textContent = `${today}rd`;
} else {
todayDate.textContent = `${today}th`;
}
if (ST.includes(tomorrow)) {
tomorrowDate.textContent = `${tomorrow}st`;
} else if (ND.includes(tomorrow)) {
tomorrowDate.textContent = `${tomorrow}nd`;
} else if (RD.includes(tomorrow)) {
tomorrowDate.textContent = `${tomorrow}rd`;
} else {
tomorrowDate.textContent = `${tomorrow}th`;
}
if (ST.includes(afterTomorrow)) {
afterTomorrowDate.textContent = `${afterTomorrow}st`;
} else if (ND.includes(afterTomorrow)) {
afterTomorrowDate.textContent = `${afterTomorrow}nd`;
} else if (RD.includes(afterTomorrow)) {
afterTomorrowDate.textContent = `${afterTomorrow}rd`;
} else {
afterTomorrowDate.textContent = `${afterTomorrow}th`;
}
dateForecastDiv.appendChild(todayDate);
dateForecastDiv.appendChild(tomorrowDate);
dateForecastDiv.appendChild(afterTomorrowDate);
}
function nextTwoDaysForecast(today, tomorrow, afterTomorrow) {
nextTwoDaysDiv.innerHTML = "";
todayReading = today;
tomorrowReading = tomorrow;
afterTomorrowReading = afterTomorrow;
todayForecast.textContent = `${today}%`;
tomorrowForecast.textContent = `${tomorrow}%`;
afterTomorrowForecast.textContent = `${afterTomorrow}%`;
nextTwoDaysDiv.appendChild(todayForecast);
nextTwoDaysDiv.appendChild(tomorrowForecast);
nextTwoDaysDiv.appendChild(afterTomorrowForecast);
}
function timeZoneArea(timeLocale) {
const optionsTime = {
timeZone: timeLocale,
hour: "numeric",
minute: "numeric",
hour12: false,
};
const formattedTime = new Intl.DateTimeFormat([], optionsTime);
timeZone.textContent = `${formattedTime.format(new Date())}`;
timeCheck = formattedTime.format(new Date()).split(":")[0];
}
function updateLocation(locationName, countryName) {
locationHeading.innerHTML = `<u>${countryName}, ${locationName}</u><br><br>`;
header.textContent = "";
}
function updateTemperature(temperatureC, temperatureF) {
tempC.textContent = `${temperatureC}°C /`;
tempF.textContent = `${temperatureF}°F 🌡`;
if (temperatureC >= 25) {
tempC.style.color = "Red";
tempF.style.color = "Red";
} else if (temperatureC >= 15) {
tempC.style.color = "Orange";
tempF.style.color = "Orange";
} else {
tempC.style.color = "Blue";
tempF.style.color = "Blue";
}
}
function windSpeed(kph, mph) {
windKph.textContent = `${kph} KPH`;
windMph.textContent = `${mph} MPH 🍃`;
if (kph <= 1) {
windStatus.textContent = "CALM";
} else if (kph <= 15) {
windStatus.textContent = "LIGHT BREEZE";
} else if (kph <= 30) {
windStatus.textContent = "MODERATE BREEZE";
} else if (kph <= 50) {
windStatus.textContent = "STRONG BREEZE";
} else if (kph <= 75) {
windStatus.textContent = "GALE";
} else if (kph <= 117) {
windStatus.textContent = "STORM";
} else {
windStatus.textContent = "HURRICANE";
}
}
function rainChanceBorder() {
rainChance.innerHTML = "";
rainChance.textContent = "Rain or Snow Chance";
rainChance.style.borderBottom = "1px solid black";
}
function forecastIcons(today, tomorrow, afterTomorrow) {
forecastDivMain.innerHTML = "";
let iconToday = document.createElement("img");
let iconTomorrow = document.createElement("img");
let iconAfterTomorrow = document.createElement("img");
iconToday.classList.add("weather-icons");
iconTomorrow.classList.add("weather-icons");
iconAfterTomorrow.classList.add("weather-icons");
// Today's weather condition
if (
today === 1 &&
(WEATHER.includes("cloud") ||
WEATHER.includes("overcast") ||
WEATHER.includes("rain") ||
WEATHER.includes("patch") ||
WEATHER.includes("mist"))
) {
iconToday.src = "animated/rainy-5.svg";
iconToday.alt = "Cloudy & Rainy";
} else if (today === 1 && WEATHER.includes("thunder")) {
iconToday.src = "animated/thunder.svg";
iconToday.alt = "Thunder & Rainy";
} else if (today === 1 && WEATHER.includes("sunny")) {
iconToday.src = "animated/rainy-1.svg";
iconToday.alt = "Sunny & Rainy";
} else if (
todaySnowChance === 1 &&
(WEATHER.includes("snow") ||
WEATHER.includes("sleet") ||
WEATHER.includes("blizzard") ||
WEATHER.includes("ice"))
) {
iconToday.src = "animated/snowy-6.svg";
iconToday.alt = "Snow";
} else if (
today === 0 &&
(WEATHER.includes("clear") || WEATHER.includes("sunny"))
) {
iconToday.src = "animated/day.svg";
iconToday.alt = "Clear";
} else if (today === 0 && WEATHER.includes("cloud")) {
iconToday.src = "animated/cloudy.svg";
iconToday.alt = "Cloud";
} else if (
today === 0 &&
(WEATHER.includes("rain") || WEATHER.includes("patch"))
) {
iconToday.src = "animated/rainy-4.svg";
iconToday.alt = "Light Rain";
} else {
iconToday.src = "animated/day.svg";
iconToday.alt = "Clear";
}
forecastDivMain.appendChild(iconToday);
// Tomorrow's weather condition
if (
tomorrow === 1 &&
(WEATHER.includes("cloud") ||
WEATHER.includes("overcast") ||
WEATHER.includes("rain") ||
WEATHER.includes("patch") ||
WEATHER.includes("mist"))
) {
iconTomorrow.src = "animated/rainy-5.svg";
iconTomorrow.alt = "Cloudy & Rainy";
} else if (tomorrow === 1 && WEATHER.includes("thunder")) {
iconTomorrow.src = "animated/thunder.svg";
iconTomorrow.alt = "Thunder & Rainy";
} else if (tomorrow === 1 && WEATHER.includes("sunny")) {
iconTomorrow.src = "animated/rainy-1.svg";
iconTomorrow.alt = "Sunny & Rainy";
} else if (
tomorrowSnowChance === 1 &&
(WEATHER.includes("snow") ||
WEATHER.includes("sleet") ||
WEATHER.includes("blizzard") ||
WEATHER.includes("ice"))
) {
iconTomorrow.src = "animated/snowy-6.svg";
iconTomorrow.alt = "Snow";
} else if (
tomorrow === 0 &&
(WEATHER.includes("clear") || WEATHER.includes("sunny"))
) {
iconTomorrow.src = "animated/day.svg";
iconTomorrow.alt = "Clear";
} else if (tomorrow === 0 && WEATHER.includes("cloud")) {
iconTomorrow.src = "animated/cloudy.svg";
iconTomorrow.alt = "Cloud";
} else if (
tomorrow === 0 &&
(WEATHER.includes("rain") || WEATHER.includes("patch"))
) {
iconTomorrow.src = "animated/rainy-4.svg";
iconTomorrow.alt = "Light Rain";
} else {
iconTomorrow.src = "animated/day.svg";
iconTomorrow.alt = "Clear";
}
forecastDivMain.appendChild(iconTomorrow);
// After-tomorrow's weather condition
if (
afterTomorrow === 1 &&
(WEATHER.includes("cloud") ||
WEATHER.includes("overcast") ||
WEATHER.includes("rain") ||
WEATHER.includes("thunder") ||
WEATHER.includes("patch") ||
WEATHER.includes("mist"))
) {
iconAfterTomorrow.src = "animated/rainy-5.svg";
iconAfterTomorrow.alt = "Cloudy & Rainy";
} else if (afterTomorrow === 1 && WEATHER.includes("thunder")) {
iconAfterTomorrow.src = "animated/thunder.svg";
iconAfterTomorrow.alt = "Thunder & Rainy";
} else if (afterTomorrow === 1 && WEATHER.includes("sunny")) {
iconAfterTomorrow.src = "animated/rainy-1.svg";
iconAfterTomorrow.alt = "Sunny & Rainy";
} else if (
afterTomorrowSnowChance === 1 &&
(WEATHER.includes("snow") ||
WEATHER.includes("sleet") ||
WEATHER.includes("blizzard") ||
WEATHER.includes("ice"))
) {
iconAfterTomorrow.src = "animated/snowy-6.svg";
iconAfterTomorrow.alt = "Snow";
} else if (
afterTomorrow === 0 &&
(WEATHER.includes("clear") || WEATHER.includes("Sunny"))
) {
iconAfterTomorrow.src = "animated/day.svg";
iconAfterTomorrow.alt = "Clear";
} else if (afterTomorrow === 0 && WEATHER.includes("cloud")) {
iconAfterTomorrow.src = "animated/cloudy.svg";
iconAfterTomorrow.alt = "Cloud";
} else if (
today === 0 &&
(WEATHER.includes("rain") || WEATHER.includes("patch"))
) {
iconAfterTomorrow.src = "animated/rainy-4.svg";
iconAfterTomorrow.alt = "Light Rain";
} else {
iconAfterTomorrow.src = "animated/day.svg";
iconAfterTomorrow.alt = "Clear";
}
forecastDivMain.appendChild(iconAfterTomorrow);
console.log(afterTomorrow, WEATHER);
}
searchBtn.addEventListener("click", function () {
const CITY_PICKED = cityInput.value;
fetchWeatherData(CITY_PICKED);
fetchForecast(CITY_PICKED);
cityInput.value = "";
});
cityInput.addEventListener("keydown", function (e) {
if (e.key === "Enter") {
const CITY_PICKED = cityInput.value;
fetchWeatherData(CITY_PICKED);
fetchForecast(CITY_PICKED);
cityInput.value = "";
}
});
});