-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
83 lines (63 loc) · 2.26 KB
/
Copy pathscript.js
File metadata and controls
83 lines (63 loc) · 2.26 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
async function getWeather() {
const city = document.getElementById("city").value.trim();
const result = document.getElementById("result");
if (!city) {
result.innerHTML = "<h3>Please enter a city name!</h3>";
return;
}
const apiKey = "ac2b2a5b5dbb2242ffb3fbffd6cf6ff1";
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
try {
const response = await fetch(url);
const data = await response.json();
console.log(data);
if (data.cod != 200) {
result.style.display = "block";
result.innerHTML = "<h2>City Not Found!</h2>";
return;
}
const cityName = data.name;
const temp = data.main.temp;
const humidity = data.main.humidity;
const wind = data.wind.speed;
const weather = data.weather[0].description;
// let emoji = "🌍";
// if (weather === "Clear") {
// emoji = "☀️";
// }
// else if (weather === "Clouds") {
// emoji = "☁️";
// }
// else if (weather === "Rain") {
// emoji = "🌧️";
// }
// else if (weather === "Mist") {
// emoji = "🌫️";
// }
// else if (weather === "Thunderstorm") {
// emoji = "⛈️";
// }
// else if (weather === "Snow") {
// emoji = "❄️";
// }
result.style.display = "block";
document.getElementById("result").innerHTML = `
<h2>${cityName}</h2>
<img src="https://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png" alt="weather-icon">
<h3>${weather}</h3>
<p>🌡️ Temperature: ${temp}°C</p>
<p>💧 Humidity: ${humidity}%</p>
<p>💨 Wind: ${wind} m/s</p>
`;
} catch (error) {
result.innerHTML = "<h2>Something went wrong! Try again.</h2>";
console.log(error);
}
}
// ye wala code ka kaam h ki placeholder me ja ke koi user enter pr click kre to search ho bs!
const cityInput = document.getElementById("city");
cityInput.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
getWeather();
}
});