-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
50 lines (37 loc) · 1.48 KB
/
script.js
File metadata and controls
50 lines (37 loc) · 1.48 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
const resultDiv = document.getElementById("result");
async function fetchCharacters() {
let characterId = 1;
for (let i = 0; i < 41; i++) {
console.log("Personagem ID atual:", characterId);
const url = `https://swapi.dev/api/people/${characterId}/`;
try {
const response = await fetch(url);
if (!response.ok) {
console.warn(`Personagem ${characterId} não encontrado (Erro ${response.status}).`);
characterId++;
continue;
}
const data = await response.json();
const card = document.createElement("div");
card.className = "card";
card.innerHTML = `
<h2>${data.name}</h2>
<p><strong>Altura:</strong> ${data.height} cm</p>
<p><strong>Peso:</strong> ${data.mass} kg</p>
<p><strong>Cor do cabelo:</strong> ${data.hair_color}</p>
<p><strong>Cor dos olhos:</strong> ${data.eye_color}</p>
<p><strong>Ano de nascimento:</strong> ${data.birth_year}</p>
<p><strong>Gênero:</strong> ${data.gender}</p>
`;
resultDiv.appendChild(card);
} catch (error) {
console.error(`Erro ao buscar o personagem ${characterId}:`, error);
const errorMsg = document.createElement("p");
errorMsg.style.color = "red";
errorMsg.textContent = `Erro ao buscar o personagem ${characterId}. Tente novamente mais tarde.`;
resultDiv.appendChild(errorMsg);
}
characterId++;
}
}
document.addEventListener("DOMContentLoaded", fetchCharacters);