Skip to content

Commit 674994c

Browse files
committed
add week1 homework
1 parent cb27f4c commit 674994c

14 files changed

Lines changed: 250 additions & 6 deletions

Week1/homework/app.js

Lines changed: 199 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,206 @@
11
'use strict';
2-
{
2+
3+
function main() {
34
const bookTitles = [
45
// Replace with your own book titles
5-
'harry_potter_chamber_secrets'
6+
'harry_potter_chamber_secrets',
7+
'eloquent_java_script',
8+
'ccna_routing_and_switching',
9+
'from_tadmor_to_harvard',
10+
'introduction_to_automata_theory',
11+
'distributed_systems_concepts_and_design',
12+
'harry_potter_sorcerers_stone',
13+
'harry_potter_deathly_hallows',
14+
'harry_potter_cursed_child',
15+
'harry_potter_half_blood_prince'
616
];
717

18+
const booksInfo = {
19+
harry_potter_chamber_secrets: {
20+
title: 'Harry Potter - The Chamber of Secrets',
21+
language: 'English',
22+
author: 'J. K. Rowling',
23+
publisher: 'Scholastic Paperbacks; Reprint edition (September 1, 2000)',
24+
paperback: '341 pages',
25+
isbn: '0439064872',
26+
amazon: 'https://www.amazon.com/Harry-Potter-Chamber-Secrets-Rowling/dp/0439064872/'
27+
},
28+
eloquent_java_script: {
29+
title: 'Eloquent JavaScript',
30+
language: 'English',
31+
author: 'Marijn Haverbeke',
32+
publisher: 'No Starch Press; 2 edition (December 7, 2014)',
33+
paperback: '472 pages',
34+
isbn: '1593275846',
35+
amazon: 'https://www.amazon.com/Eloquent-JavaScript-2nd-Ed-Introduction/dp/1593275846/'
36+
},
37+
from_tadmor_to_harvard: {
38+
title: 'From Tadmor to Harvard',
39+
language: 'Arabic',
40+
author: 'Bara Sarraj',
41+
publisher: 'CreateSpace Independent Publishing Platform (February 2, 2016)',
42+
paperback: '318 pages',
43+
isbn: '1523402970',
44+
amazon: 'https://www.amazon.com/Tadmor-Harvard-Arabic-Bara-Sarraj/dp/1523402970'
45+
},
46+
ccna_routing_and_switching: {
47+
title: 'CCNA Routing and Switching',
48+
language: 'English',
49+
author: 'Wendell Odom',
50+
publisher: 'Cisco Press; 1 edition (August 5, 2016)',
51+
paperback: '1600 pages',
52+
isbn: '1587205815',
53+
amazon: 'https://www.amazon.com/Routing-Switching-200-125-Official-Library/dp/1587205815/'
54+
},
55+
introduction_to_automata_theory: {
56+
title: 'Introduction to Automata Theory, Languages and Computation',
57+
language: 'English',
58+
author: 'John E. Hopcroft & Jeffrey D. Ullman',
59+
publisher: 'Addison-Wesley Publishing Company; 1st edition (April 1979)',
60+
paperback: '500 pages',
61+
isbn: '020102988X',
62+
amazon: 'https://www.amazon.com/Introduction-Automata-Languages-Computation-Addison-Wesley/dp/020102988X/'
63+
},
64+
distributed_systems_concepts_and_design: {
65+
title: 'Distributed Systems: Concepts and Design',
66+
language: 'English',
67+
author: 'George Coulouris',
68+
publisher: 'Pearson; 5 edition (May 7, 2011)',
69+
paperback: '1008 pages',
70+
isbn: '0132143011',
71+
amazon: 'https://www.amazon.com/Distributed-Systems-Concepts-Design-5th/dp/0132143011/'
72+
},
73+
harry_potter_sorcerers_stone: {
74+
title: 'Harry Potter and the Sorcerer\'s Stone',
75+
language: 'English',
76+
author: 'J. K. Rowling',
77+
publisher: 'Scholastic; 1st Edition edition (September 1998)',
78+
paperback: '309 pages',
79+
isbn: '0439708184',
80+
amazon: 'https://www.amazon.com/Harry-Potter-Sorcerers-Stone-Rowling/dp/059035342X'
81+
},
82+
harry_potter_deathly_hallows: {
83+
title: 'Harry Potter and the Deathly Hallows',
84+
language: 'English',
85+
author: 'J. K. Rowling',
86+
publisher: 'Arthur A. Levine Books (July 1, 2009)',
87+
paperback: '784 pages',
88+
isbn: '0545139708',
89+
amazon: 'https://www.amazon.com/Harry-Potter-Deathly-Hallows-Book/dp/0545139708'
90+
},
91+
harry_potter_cursed_child: {
92+
title: 'Harry Potter and the Cursed Child',
93+
language: 'English',
94+
author: 'J. K. Rowling',
95+
publisher: 'Arthur A. Levine Books (July 25, 2017)',
96+
paperback: '336 pages',
97+
isbn: '133821666X',
98+
amazon: 'https://www.amazon.com/Harry-Potter-Cursed-Child-Parts/dp/133821666X'
99+
},
100+
harry_potter_half_blood_prince: {
101+
title: 'Harry Potter and the Half-Blood Prince',
102+
language: 'English',
103+
author: 'J. K. Rowling',
104+
publisher: 'Scholastic Paperbacks; Reprint edition (July 25, 2006)',
105+
paperback: '652 pages',
106+
isbn: '0439785960',
107+
amazon: 'https://www.amazon.com/Harry-Potter-Half-Blood-Prince-Book/dp/0439785960'
108+
},
109+
};
110+
111+
const body = document.body; // get body element
112+
body.appendChild(createList(bookTitles));
113+
114+
function createList(books) {
115+
const booksList = document.createElement('ul');
116+
117+
for (let i = 0; i < books.length; i++) { // create list item for each book.
118+
119+
const item = document.createElement('li');
120+
item.id = books[i];
121+
item.appendChild(createChild('h2', booksInfo[books[i]].title));
122+
item.appendChild(createChild('p', 'By ', createChild('em', booksInfo[books[i]].author)));
123+
booksList.appendChild(item);
124+
125+
const bookDetails = document.createElement('ul');
126+
item.appendChild(bookDetails);
127+
128+
for (const pro in booksInfo[books[i]]) {
129+
130+
if (pro === 'amazon') { // add amazon link.
131+
132+
const link = createChild('a', capitalizeFirstLetter(pro));
133+
link.href = booksInfo[books[i]][pro];
134+
link.target = '_blank';
135+
bookDetails.appendChild(createChild('li', link, '.'));
136+
137+
} else if (pro === 'isbn') { // isbn should be in uppercase.
138+
139+
bookDetails.appendChild(createChild('li', createChild('b', pro.toUpperCase(), ': '), booksInfo[books[i]][pro], '.'));
140+
141+
} else if (pro !== 'title' && pro !== 'author') { //Don't add title and author, already added before.
142+
143+
bookDetails.appendChild(createChild('li', createChild('b', capitalizeFirstLetter(pro), ': '), booksInfo[books[i]][pro], '.'));
144+
145+
}
146+
147+
}
148+
149+
}
150+
151+
return booksList;
152+
}
153+
154+
//function to create an element with specific TYPE and add CHILDREN to it.
155+
//CHILDREN could be string or node (which could be an another createChild() call).
156+
function createChild(type, ...children) {
157+
const node = document.createElement(type);
158+
159+
for (const child of children) {
160+
161+
if (typeof child === 'string') { // append string child as a text node.
162+
node.appendChild(document.createTextNode(child));
163+
} else { // else it's a node.
164+
node.appendChild(child);
165+
}
166+
167+
}
168+
169+
return node;
170+
}
171+
172+
const booksCover = buildImagesObj(bookTitles); // create images object.
173+
addImage(booksCover); // add images to the page.
174+
175+
function buildImagesObj(books) {
176+
const obj = new Object();
177+
178+
for (let i = 0; i < books.length; i++) {
179+
obj[books[i]] = './img/' + books[i] + '.jpg';
180+
}
181+
182+
return obj;
183+
}
184+
185+
function addImage(images) {
186+
187+
for (const image in images) {
188+
189+
const li = document.getElementById(image); // get the list item that it's id == image name.
190+
const img = document.createElement('img');
191+
img.src = images[image];
192+
img.alt = image;
193+
194+
const selector = '#' + image + ' > ul'; // select details list.
195+
li.insertBefore(img, document.querySelector(selector));// add image before details list.
196+
197+
}
198+
}
199+
200+
function capitalizeFirstLetter(string) {
201+
return string.charAt(0).toUpperCase() + string.slice(1);
202+
}
8203

9-
// Replace with your own code
10-
console.log(bookTitles);
11204
}
205+
206+
window.addEventListener('load', main);
39.9 KB
Loading
38.7 KB
Loading
40.8 KB
Loading

Week1/homework/img/favicon.png

1.86 KB
Loading
30.6 KB
Loading
48.5 KB
Loading
57.4 KB
Loading
37.3 KB
Loading
44.3 KB
Loading

0 commit comments

Comments
 (0)