-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
38 lines (30 loc) · 986 Bytes
/
script.js
File metadata and controls
38 lines (30 loc) · 986 Bytes
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
const apiKey = "SUA_API_KEY";
async function buscarClima() {
const cidade = document.querySelector("#cidade").value;
const container = document.querySelector("#resultado");
if (cidade === "") {
container.innerHTML = "<p>Digite uma cidade.</p>";
return;
}
try {
const dados = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${cidade}&appid=${apiKey}&units=metric&lang=pt_br`
);
if (!dados.ok) {
throw new Error("Cidade não encontrada");
}
const resposta = await dados.json();
container.innerHTML = `
<div class="card-clima">
<h2>${resposta.name}</h2>
<p>🌡️ Temperatura: ${Math.round(resposta.main.temp)}°C</p>
<p>☁️ Clima: ${resposta.weather[0].description}</p>
<p>💧 Umidade: ${resposta.main.humidity}%</p>
</div>
`;
} catch (erro) {
container.innerHTML = `
<p style="color:white;">Erro: cidade não encontrada.</p>
`;
}
}