-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
52 lines (40 loc) · 1.72 KB
/
script.js
File metadata and controls
52 lines (40 loc) · 1.72 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
document.addEventListener("DOMContentLoaded", function() {
const btn = document.getElementById("button1");
btn.onclick = function() {
function randNum() {
return randomNum = Math.floor(Math.random() * 83) };
pageNum = randNum();
fetch(`https://swapi.info/api/people/${pageNum}`)
.then(response => response.json())
.then(data => {
console.log(data);
const name1 = data.name;
const nameElement = document.getElementById("name");
nameElement.textContent = name1;
fetch(data.homeworld)
.then(response => response.json())
.then(data => {
console.log(data);
const homeworld1 = data.name;
const homeworldElement = document.getElementById("planet");
homeworldElement.textContent = homeworld1;
})
const itemToRemove = document.querySelectorAll("li");
if (itemToRemove) {
itemToRemove.forEach(item => item.remove()); }
if (data.films.length > 0) {
for (let i = 0; i < data.films.length; i++) {
fetch(data.films[i])
.then(response => response.json())
.then(data => {
console.log(data);
const film1 = data.title;
const filmElement = document.getElementById("movies");
const filmListItem = document.createElement("li");
filmListItem.textContent = film1;
filmElement.appendChild(filmListItem);
})
}
}})
};
});