forked from HackYourFuture/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (59 loc) · 2.03 KB
/
index.js
File metadata and controls
68 lines (59 loc) · 2.03 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
//cspell: disable
/*------------------------------------------------------------------------------
Full description at: https://github.com/HackYourFuture/Assignments/tree/main/2-Browsers/Week1#exercise-1-the-book-list
I'd like to display my three favorite books inside a nice webpage!
1. Iterate through the array of books.
2. For each book, create a `<p>`
element with the book title and author.
3. Use a `<ul>` and `<li>` to display the books.
4. Add an `<img>` to each book that links to a URL of the book cover.
5. Change the style of the book depending on whether you have read it(green) or not(red).
The end result should look something like this:
https://hackyourfuture.github.io/example-pages/Browsers/Week1/1-booklist/
-----------------------------------------------------------------------------*/
//cspell: enable
function createBookList(books) {
const ul = document.createElement ('ul');
books.forEach( book => {
const li = document.createElement ('li');
const p = document.createElement ( 'p');
p.textContent = `${book.title} - ${book.author} `;
const img = document.createElement ('img');
img.src = `https://covers.openlibrary.org/b/isbn/${book.isbn}-M.jpg`;
img.alt = `${book.title} cover`;
li.appendChild (p);
li.appendChild (img);
if (book.alreadyRead) {
li.style.backgroundColor = 'green';
}
else
{ li.style.backgroundColor = 'red'} ;
ul.appendChild (li);
});
return ul;
}
function main() {
const myBooks = [
{
title: 'The Design of Everyday Things',
author: 'Don Norman',
isbn: '978-0465050659',
alreadyRead: false,
},
{
title: 'The Most Human Human',
author: 'Brian Christian',
isbn: '978-1617933431',
alreadyRead: true,
},
{
title: 'The Pragmatic Programmer',
author: 'Andrew Hunt',
isbn: '978-0201616224',
alreadyRead: true,
},
];
const ulElement = createBookList(myBooks);
document.querySelector('#bookList').appendChild(ulElement);
}
window.addEventListener('load', main);