Skip to content

Commit 4f022b6

Browse files
committed
cleaned the code by reducing unecessary lines
1 parent 9d7e7a7 commit 4f022b6

File tree

1 file changed

+27
-52
lines changed

1 file changed

+27
-52
lines changed

Day-24-Weather-App/script.js

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
//Selection of Elements
2-
const currentWeather = document.querySelector(".current-weather");
3-
const weatherForeCast = document.querySelector(".weather-forecast");
4-
let cityName = document.querySelector("input");
5-
const button = document.querySelector("button");
6-
const para = document.querySelector(".currTime");
7-
const extraInfo = document.querySelector(".extraInfo");
8-
const temp = document.querySelector(".temp");
9-
const inputVal = document.querySelector(".inputVal");
10-
const description = document.querySelector(".description");
111
const API = "e594f5869719e24b7089dcc31a97c76f"; //Put your API in here
2+
let cityName = document.querySelector("input");
123

13-
const dataFecth = async function () {
4+
async function dataFecth() {
145
try {
156
const response = await fetch(
167
`https://api.openweathermap.org/data/2.5/weather?q=${cityName.value}&appid=${API}`
@@ -23,7 +14,7 @@ const dataFecth = async function () {
2314
} catch (error) {
2415
console.error("Error aa gya:", error);
2516
}
26-
};
17+
}
2718

2819
async function fetchForeCast() {
2920
try {
@@ -41,41 +32,25 @@ async function fetchForeCast() {
4132
}
4233
}
4334

44-
function getTime() {
45-
let time = new Date();
46-
let IndiaTime = time.toLocaleTimeString("en-In", {
47-
timeZone: "Asia/Kolkata",
48-
});
49-
return IndiaTime;
50-
}
51-
function startInterval() {
52-
setInterval(() => {
53-
document.querySelector("#getTime").textContent = getTime();
54-
}, 1000);
55-
}
56-
57-
button.addEventListener("click", () => {
35+
document.querySelector("button").addEventListener("click", () => {
5836
dataFecth();
59-
startInterval();
6037
fetchForeCast();
6138
});
6239

6340
function displayCurrentData(data) {
64-
const p = document.createElement("p");
65-
const img = document.createElement("img");
66-
const mT1 = document.createElement("p");
67-
const mT2 = document.createElement("p");
68-
const C = document.createElement("span");
69-
C.textContent = Math.round(data?.main?.temp - 273.15) + " °C";
70-
p.textContent = "Current Weather";
71-
img.src = `https://openweathermap.org/img/wn/${data.weather[0].icon}.png`;
72-
mT1.textContent = data.weather[0].main;
73-
mT2.textContent = `Feels Like ${Math.round(
74-
data.main.feels_like - 273.15
75-
)} °C`;
76-
para.insertBefore(p, para.firstChild);
77-
extraInfo.append(mT1, mT2);
78-
temp.append(img, C);
41+
const currentWeather = document.querySelector(".current-weather");
42+
currentWeather.innerHTML = `
43+
<p id="currTime">Current Weather</p>
44+
<div class="temp">
45+
<img src="https://openweathermap.org/img/wn/${data.weather[0].icon}.png">
46+
<span>${Number(data?.main?.temp - 273.15).toFixed(2)}°C</span>
47+
</div>
48+
<div class="extraInfo">
49+
<p>${data.weather[0].main}</p>
50+
<p>Feels Like ${Number(data.main.feels_like - 273.15).toFixed(2)} °C</p>
51+
</div>
52+
`;
53+
const inputVal = document.querySelector(".inputVal");
7954
inputVal.textContent = cityName.value;
8055
}
8156

@@ -88,23 +63,23 @@ function getForeastData(data) {
8863
}
8964

9065
function displayForeCast(data) {
66+
const weatherForeCast = document.querySelector(".weather-forecast");
67+
const description = document.querySelector(".description");
9168
const p = document.createElement("p");
9269
p.id = "check";
9370
p.textContent = `5 Day Weather Forecast for city ${cityName.value}`;
9471
description.appendChild(p);
9572
for (let i = 0; i < data.length; i++) {
96-
const div = document.createElement("div");
97-
const temp = document.createElement("p");
98-
const date = document.createElement("p");
99-
const img = document.createElement("img");
100-
date.textContent = new Date(data[i].dt * 1000).toLocaleDateString("en-In", {
73+
let date = new Date(data[i].dt * 1000).toLocaleDateString("en-In", {
10174
day: "numeric",
10275
month: "short",
10376
});
104-
img.src = `https://openweathermap.org/img/wn/${data[i].weather[0].icon}.png`;
105-
temp.textContent = Number(data[i]?.main?.temp - 273.15).toFixed(2) + " °C";
106-
div.id = "detail";
107-
div.append(date, img, temp);
108-
weatherForeCast.append(div);
77+
weatherForeCast.innerHTML += `
78+
<div id="detail">
79+
<p>${date}</p>
80+
<img src="${`https://openweathermap.org/img/wn/${data[i].weather[0].icon}.png`}">
81+
<p>${Number(data[i]?.main?.temp - 273.15).toFixed(2) + "°C"}</p>
82+
</div>
83+
`;
10984
}
11085
}

0 commit comments

Comments
 (0)