-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
69 lines (47 loc) · 1.66 KB
/
scripts.js
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
//var data = '[ { "name": "Aragorn", "race": "Human" }, {"name": "Gimli", "race": "Dwarf"} ]'
// parse out the JSON data
//data = JSON.parse(data);
//console.log(data[1].name)
const app = document.getElementById('root')
console.log(app)
const logo = document.createElement('img')
logo.src = 'logo.png'
const container = document.createElement('div')
container.setAttribute('class', 'container')
app.appendChild(logo)
app.appendChild(container)
const url = 'https://ghibliapi.herokuapp.com/films'
fetch(url)
.then(response => {
return response.json()
})
.then(data => {
// Work with JSON data here
console.log(data)
//console.log(data.length)
//data.forEach(movie => {
for( i=0; i<=data.length; i++) {
// Create a div with a card class
const card = document.createElement('div')
card.setAttribute('class', 'card')
// Create an h1 and set the text content to the film's title
const h1 = document.createElement('h1')
//h1.textContent = movie.title
h1.textContent = data[i].title
// Create a p and set the text content to the film's description
const p = document.createElement('p')
//movie.description = movie.description.substring(0,300) // Limit to 300 chars
data[i].description = data[i].description.substring(0,300) // Limit to 300 chars
//p.textContent = `${movie.description}...` // End with an ellipses
p.textContent = `${data[i].description}...` // End with an ellipses
// Append the cards to the container element
container.appendChild(card)
// Each card will contain an h1 and a p
card.appendChild(h1)
card.appendChild(p)
//})
}
})
.catch(err => {
// Do something for an error here
})