forked from HackYourFuture/JavaScript2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
157 lines (125 loc) · 4.89 KB
/
Copy pathapp.js
File metadata and controls
157 lines (125 loc) · 4.89 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
const bookTitles = [
// Replace with your own book titles
'harry_potter_chamber_secrets',
'harry_potter_philosophers_stone',
'the_fiery_cross',
'madame_bovary',
'anna_karenina',
'harry_potter_goblet_of_fire',
'twilight',
'outlander',
'harry_potter_deathly_hallows',
'harry_potter_and_the_half_blood_prince'
];
/* const divForBookList = document.createElement('div'); //creates the div
const listOfBooks = document.createElement('ul'); // creates the unord list
function makeBookList(){
document.getElementsByTagName('body')[0].appendChild(divForBookList); //add the div to the body tag
divForBookList.appendChild(listOfBooks); //adds the ul to the div
for (let i = 0; i < bookTitles.length; i++){ //for each book title
const listItems = document.createElement('li'); // create a li for each of the items
listItems.innerHTML = bookTitles[i]; //add each book title index item to the listItems
listOfBooks.appendChild(listItems); //add the list items to the ul
}
}
makeBookList() */
const nameOfList = {
'harry_potter_chamber_secrets': {
'title': 'Harry Potter and the Chamber of Secrets',
language: 'English',
author: 'JK Rowling'
},
'harry_potter_philosophers_stone': {
'title': 'Harry Potter and the Philosopher\s Stone',
language: 'English',
author: 'JK Rowling'
},
'the_fiery_cross': {
'title': 'The Fiery Cross',
language: 'English',
author: 'Diana Galbaldon'
},
'madame_bovary': {
'title': 'Madame Bovary',
language: 'French',
author: 'Gustav Flaubert'
},
'anna_karenina': {
'title': 'Anna Karenina',
language: 'English',
author: 'Leo Tolstoy'
},
'harry_potter_goblet_of_fire': {
'title': 'Harry Potter and the Goblet of Fire',
language: 'English',
author: 'JK Rowling'
},
'twilight': {
'title': 'Twlight',
language: 'English',
author: 'Stephanie Meyer'
},
'outlander': {
'title': 'Outlander',
language: 'English',
author: 'Diana Galbadon'
},
'harry_potter_the_deathly_hallows': {
'title': 'Harry Potter and the Deathly Hallows',
language: 'English',
author: 'JK Rowling'
},
'harry_potter_and_the_halfblood_prince': {
'title': 'Harry Potter and the Half Blood Prince',
language: 'English',
author: 'JK Rowling'
}
}
var fieldset = document.createElement( 'fieldset' ); //creates a group
fieldset.setAttribute( 'class' , 'nameOfList' ); //adds a class to the group
var legend = document.createElement( 'legend' );
legend.innerHTML = 'My Book List';
fieldset.appendChild(legend);
function makeBookList(){
const ul = document.createElement('ul'); // creates the unord list
const myBooks = Object.keys(nameOfList) //turns object into array (ie gives index)
const div = document.createElement('div'); //creates the div
const h1second = document.createElement('h1'); // creates language h1
const h1third = document.createElement('h1'); // creates author h1
document.getElementsByTagName('body')[0].appendChild(div); //add the div to the body tag
div.appendChild(ul); //adds the ul to the div
const img = document.createElement('img'); // creates image
for( let i = 0; i < myBooks.length; i++){
const li = document.createElement('li'); //creates list
const li2 = document.createElement('li'); //creates 2nd list
var bookImage = document.createElement( 'img' ); //creates img
const h2 = document.createElement('h2'); //creates h2
const h2second = document.createElement('h2'); //creates 2nd h2
const h1 = document.createElement('h1'); // creates title h1
var bookId = myBooks[i]; //creates index of index
const myBook = nameOfList[bookId];
h1.innerHTML = myBook.title; //writes h1 to html
h2.innerHTML = ("author"); //writes h2 to html author
li.innerHTML = myBook.language; //writes property of language to li
h2second.innerHTML = ("language"); //writes 2nd h2 to html
li2.innerHTML = myBook.author; //writes property of author to li2
ul.appendChild(h1second); //below all added to ul
ul.appendChild(h1);
ul.appendChild(h1third);
ul.appendChild(h2second);
ul.appendChild(li);
ul.appendChild(h2);
ul.appendChild(li2);
div.append( bookImage );
var bookImage = document.createElement( 'img' ); // creates image
bookImage.src = 'img/' + bookTitles[ i ] + '.jpg'; //adds src of book titles array
bookImage.setAttribute( 'alt' , bookTitles[ i ] ); // adds alt to image
bookImage.setAttribute('height', '300px'); //adds height
bookImage.setAttribute('width', '200px');// adds width
ul.append( bookImage ); //adds to ul
}
fieldset.appendChild(div);
document.body.append(fieldset );
};
makeBookList();