diff --git a/index.html b/index.html new file mode 100644 index 00000000..966c60fd --- /dev/null +++ b/index.html @@ -0,0 +1,44 @@ + + +
+ + + + + + + +
';
+const clouds = '
';
+const drizzle = '
';
+const thunderStorm = '
';
+const snow = '
';
+let currentResults = [];
+let fiveDayResults = [];
+let conditionImg;
+
+submitBtn.addEventListener("click", () => {
+ document.querySelector(".current").replaceChildren();
+ document.querySelector(".five-day").replaceChildren();
+ currentResults = [];
+ fiveDayResults = [];
+ conditionImg = "";
+ let city = searchInput.value;
+
+ if (!city) {
+ alert("Please enter valid city name.");
+ } else {
+ fetchWeather(city);
+ }
+
+ searchInput.value = "";
+});
+
+async function fetchWeather(city) {
+ try {
+ const currentWeather = await fetch(
+ `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=8ecfabbc5fb6be8c3ec37ba3114514e4&units=imperial`
+ );
+ const currentWeatherData = await currentWeather.json();
+ getCurrentWeather(currentWeatherData);
+
+ const fiveDayForecast = await fetch(
+ `https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=8ecfabbc5fb6be8c3ec37ba3114514e4&units=imperial`
+ );
+ const fDFData = await fiveDayForecast.json();
+ getFiveDayForcast(fDFData);
+ } catch (error) {
+ console.error("Error fetching data");
+ alert("Weather inquiry failed.");
+ }
+}
+
+function getCurrentWeather(currentWeatherData) {
+ currentResults.push({
+ city: currentWeatherData.name,
+ temp: Math.floor(currentWeatherData.main.temp),
+ conditions: currentWeatherData.weather[0].main,
+ });
+ renderCurrentWeather(currentResults);
+}
+
+function getFiveDayForcast(fDFData) {
+ const fiveDayHourly = fDFData.list;
+ for (let i = 0; i < fiveDayHourly.length; i = i + 8) {
+ fiveDayResults.push(fiveDayHourly[i]);
+ }
+ renderFiveDayForecast(fiveDayResults);
+}
+
+function renderCurrentWeather(currentResults) {
+ const temp = currentResults[0].city;
+ const city = currentResults[0].temp;
+ let conditions = currentResults[0].conditions;
+
+ for (let i = 0; i < currentResults.length; i++) {
+ if (conditions === "Clouds") {
+ conditionImg = clouds;
+ } else if (conditions === "Rain") {
+ conditionImg = rain;
+ } else if (conditions === "Clear") {
+ conditionImg = clear;
+ } else if (conditions === "Drizzle") {
+ conditionImg = drizzle;
+ } else if (conditions === "Thunderstorm") {
+ conditions = thunderStorm;
+ } else if (conditions === "Snow") {
+ conditionImg = snow;
+ } else {
+ conditionImg = mist;
+ }
+ const template = `
+ ${conditions}
+${dailyForecasts[0].day}
+${dailyForecasts[0].conditions}
+${dailyForecasts[1].day}
+${dailyForecasts[1].conditions}
+${dailyForecasts[2].day}
+${dailyForecasts[2].conditions}
+${dailyForecasts[3].day}
+${dailyForecasts[3].conditions}
+${dailyForecasts[4].day}
+${dailyForecasts[3].conditions}
+