1- const myLibrary = [ ] ;
1+ let myLibrary = [ ] ;
22
33function Books ( title , author , pages , status ) {
44 this . title = title ;
@@ -7,50 +7,27 @@ function Books(title, author, pages, status) {
77 this . status = status ;
88}
99
10- function addBookToLibrary ( title , author , pages , status ) {
11- const book = new Books ( title , author , pages , status ) ;
12- myLibrary . push ( book ) ;
13- return myLibrary ;
14- }
15- addBookToLibrary ( 'Sapiens: A Brief History of Humankind' , 'Yuval Noah Harari' , '464' , 'read' ) ;
16- addBookToLibrary ( 'The Lady of the Lake' , 'Andrzej Sapkowski' , '544' , 'unread' ) ;
17-
18- function showLibraryInfo ( ) {
19- let readCounter = 0 ;
20- let unreadCounter = 0 ;
21- for ( let i = 0 ; i < myLibrary . length ; i += 1 ) {
22- if ( myLibrary [ i ] . status === 'read' ) {
23- readCounter += 1 ;
24- const booksRead = document . querySelector ( '#books-read' ) ;
25- booksRead . textContent = readCounter ;
26- } else if ( myLibrary [ i ] . status === 'unread' ) {
27- unreadCounter += 1 ;
28- const booksUnread = document . querySelector ( '#books-unread' ) ;
29- booksUnread . textContent = unreadCounter ;
30- }
31- }
32- const totalBooks = document . querySelector ( '#total-books' ) ;
33- totalBooks . textContent = myLibrary . length ;
34- }
35- showLibraryInfo ( ) ;
36-
3710function showBooksInLibrary ( ) {
11+ const bookList = document . querySelector ( '#table-body' ) ;
12+ bookList . textContent = '' ;
3813 for ( let i = 0 ; i < myLibrary . length ; i += 1 ) {
39- const bookList = document . querySelector ( '#table-body' ) ;
4014 const bookRow = document . createElement ( 'tr' ) ;
4115 bookRow . classList . add ( 'book-info' ) ;
4216 bookList . appendChild ( bookRow ) ;
4317
4418 const bookTitle = document . createElement ( 'td' ) ;
4519 bookTitle . textContent = myLibrary [ i ] . title ;
20+ bookTitle . classList . add ( 'book-title' ) ;
4621 bookRow . appendChild ( bookTitle ) ;
4722
4823 const bookAuthor = document . createElement ( 'td' ) ;
4924 bookAuthor . textContent = myLibrary [ i ] . author ;
25+ bookAuthor . classList . add ( 'book-author' ) ;
5026 bookRow . appendChild ( bookAuthor ) ;
5127
5228 const bookPages = document . createElement ( 'td' ) ;
5329 bookPages . textContent = myLibrary [ i ] . pages ;
30+ bookPages . classList . add ( 'book-pages' ) ;
5431 bookRow . appendChild ( bookPages ) ;
5532
5633 const bookStatus = document . createElement ( 'td' ) ;
@@ -61,6 +38,7 @@ function showBooksInLibrary() {
6138 } else if ( myLibrary [ i ] . status === 'unread' ) {
6239 statusSymbol . classList . add ( 'fas' , 'fa-times' ) ;
6340 }
41+ bookStatus . classList . add ( 'book-status' ) ;
6442 statusButton . appendChild ( statusSymbol ) ;
6543 bookStatus . appendChild ( statusButton ) ;
6644 bookRow . appendChild ( bookStatus ) ;
@@ -70,8 +48,53 @@ function showBooksInLibrary() {
7048 const deleteSymbol = document . createElement ( 'i' ) ;
7149 deleteSymbol . classList . add ( 'fas' , 'fa-trash-alt' ) ;
7250 deleteButton . appendChild ( deleteSymbol ) ;
51+ bookDelete . classList . add ( 'book-delete' ) ;
7352 bookDelete . appendChild ( deleteButton ) ;
7453 bookRow . appendChild ( bookDelete ) ;
7554 }
7655}
56+
57+ function showLibraryInfo ( ) {
58+ const booksRead = document . querySelector ( '#books-read' ) ;
59+ const booksUnread = document . querySelector ( '#books-unread' ) ;
60+ const totalBooks = document . querySelector ( '#total-books' ) ;
61+ let readCounter = 0 ;
62+ let unreadCounter = 0 ;
63+ if ( myLibrary . length === 0 ) {
64+ booksRead . textContent = '0' ;
65+ booksUnread . textContent = '0' ;
66+ } else {
67+ for ( let i = 0 ; i < myLibrary . length ; i += 1 ) {
68+ if ( myLibrary [ i ] . status === 'read' ) {
69+ readCounter += 1 ;
70+ booksRead . textContent = readCounter ;
71+ } else if ( myLibrary [ i ] . status === 'unread' ) {
72+ unreadCounter += 1 ;
73+ booksUnread . textContent = unreadCounter ;
74+ }
75+ }
76+ }
77+ totalBooks . textContent = myLibrary . length ;
78+ }
79+
80+ function addBookToLibrary ( title , author , pages , status ) {
81+ const book = new Books ( title , author , pages , status ) ;
82+ myLibrary . push ( book ) ;
83+ showLibraryInfo ( ) ;
84+ showBooksInLibrary ( ) ;
85+ }
86+
87+ function deleteAllBooks ( ) {
88+ const deleteAllBtn = document . querySelector ( '#delete-all-btn' ) ;
89+ deleteAllBtn . addEventListener ( 'click' , ( ) => {
90+ myLibrary = [ ] ;
91+ showBooksInLibrary ( ) ;
92+ showLibraryInfo ( ) ;
93+ } ) ;
94+ }
95+
96+ addBookToLibrary ( 'Sapiens: A Brief History of Humankind' , 'Yuval Noah Harari' , '464' , 'read' ) ;
97+ addBookToLibrary ( 'The Lady of the Lake' , 'Andrzej Sapkowski' , '544' , 'unread' ) ;
98+ showLibraryInfo ( ) ;
7799showBooksInLibrary ( ) ;
100+ deleteAllBooks ( ) ;
0 commit comments