1- let myLibrary = [ ] ;
1+ const myLibrary = [ ] ;
22
33window . addEventListener ( "load" , function ( e ) {
44 populateStorage ( ) ;
55} ) ;
66
77function populateStorage ( ) {
88 if ( myLibrary . length == 0 ) {
9- let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , " 252" , true ) ;
10- let book2 = new Book (
9+ const book1 = new Book ( "Robinson Crusoe" , "Daniel Defoe" , 252 , true ) ;
10+ const book2 = new Book (
1111 "The Old Man and the Sea" ,
1212 "Ernest Hemingway" ,
13- " 127" ,
13+ 127 ,
1414 true
1515 ) ;
1616 myLibrary . push ( book1 ) ;
@@ -19,26 +19,29 @@ function populateStorage() {
1919 }
2020}
2121
22- const title = document . getElementById ( "title" ) ;
23- const author = document . getElementById ( "author" ) ;
24- const pages = document . getElementById ( "pages" ) ;
25- const check = document . getElementById ( "check" ) ;
22+ const titleInput = document . getElementById ( "title" ) ;
23+ const authorInput = document . getElementById ( "author" ) ;
24+ const pagesInput = document . getElementById ( "pages" ) ;
25+ const checkInput = document . getElementById ( "check" ) ;
2626
2727//check the right input from forms and if its ok -> add the new book (object in array)
2828//via Book function and start render function
2929function submit ( ) {
30- if (
31- title . value == "" ||
32- author . value == "" ||
33- pages . value == ""
34- ) {
30+ const title = titleInput . value . trim ( ) ;
31+ const author = authorInput . value . trim ( ) ;
32+ const pages = Number ( pagesInput . value ) ;
33+ const isRead = checkInput . checked ;
34+ if ( ! title || ! author || pagesInput . value . trim ( ) === "" ) {
3535 alert ( "Please fill all fields!" ) ;
36- return false ;
37- } else {
38- let book = new Book ( title . value , author . value , pages . value , check . checked ) ;
39- myLibrary . push ( book ) ;
40- render ( ) ;
36+ return ;
37+ }
38+ if ( Number . isNaN ( pages ) || pages <= 0 ) {
39+ alert ( "Please enter a valid number of pages!" ) ;
40+ return ;
4141 }
42+ const book = new Book ( title , author , pages , isRead ) ;
43+ myLibrary . push ( book ) ;
44+ render ( ) ;
4245}
4346
4447function Book ( title , author , pages , check ) {
@@ -48,54 +51,60 @@ function Book(title, author, pages, check) {
4851 this . check = check ;
4952}
5053
54+ function showMessage ( text ) {
55+ const msg = document . getElementById ( "message" ) ;
56+ msg . innerText = text ;
57+
58+ setTimeout ( ( ) => {
59+ msg . innerText = "" ;
60+ } , 2000 ) ;
61+ }
62+
5163function render ( ) {
52- let table = document . getElementById ( "display" ) ;
53- let rowsNumber = table . rows . length ;
64+ const table = document . getElementById ( "display" ) ;
65+
66+ table . innerHTML = table . rows [ 0 ] . outerHTML ;
67+ myLibrary . forEach ( ( book , index ) => {
68+ const row = table . insertRow ( ) ;
69+
70+ const titleCell = row . insertCell ( 0 ) ;
71+ const authorCell = row . insertCell ( 1 ) ;
72+ const pagesCell = row . insertCell ( 2 ) ;
73+ const wasReadCell = row . insertCell ( 3 ) ;
74+ const deleteCell = row . insertCell ( 4 ) ;
75+
76+ titleCell . innerText = book . title ;
77+ authorCell . innerText = book . author ;
78+ pagesCell . innerText = book . pages ;
79+
5480 //delete old table
55- for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) {
56- table . deleteRow ( n ) ;
57- }
5881 //insert updated row and cells
59- let length = myLibrary . length ;
60- for ( let i = 0 ; i < length ; i ++ ) {
61- let row = table . insertRow ( 1 ) ;
62- let titleCell = row . insertCell ( 0 ) ;
63- let authorCell = row . insertCell ( 1 ) ;
64- let pagesCell = row . insertCell ( 2 ) ;
65- let wasReadCell = row . insertCell ( 3 ) ;
66- let deleteCell = row . insertCell ( 4 ) ;
67- titleCell . innerHTML = myLibrary [ i ] . title ;
68- authorCell . innerHTML = myLibrary [ i ] . author ;
69- pagesCell . innerHTML = myLibrary [ i ] . pages ;
82+
7083
7184 //add and wait for action for read/unread button
72- let changeBut = document . createElement ( "button" ) ;
73- changeBut . id = i ;
74- changeBut . className = "btn btn-success" ;
75- wasReadCell . appendChild ( changeBut ) ;
76- let readStatus = "" ;
77- if ( myLibrary [ i ] . check == true ) {
78- readStatus = "Yes" ;
79- } else {
80- readStatus = "No" ;
81- }
82- changeBut . innerText = readStatus ;
83-
84- changeBut . addEventListener ( "click" , function ( ) {
85- myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
85+ const toggleReadButton = document . createElement ( "button" ) ;
86+ toggleReadButton . className = "btn btn-success" ;
87+ toggleReadButton . innerText = book . check ? "Yes" : "No" ;
88+
89+ toggleReadButton . addEventListener ( "click" , ( ) => {
90+ book . check = ! book . check ;
8691 render ( ) ;
8792 } ) ;
8893
94+ wasReadCell . appendChild ( toggleReadButton ) ;
95+
8996 //add delete button to every row and render again
90- let delButton = document . createElement ( "button" ) ;
91- delButton . id = i ;
92- deleteCell . appendChild ( delButton ) ;
93- delButton . className = "btn btn-warning" ;
94- delButton . innerHTML = "Delete" ;
95- delButton . addEventListener ( "click" , function ( ) {
96- alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
97- myLibrary . splice ( i , 1 ) ;
97+ const deleteButton = document . createElement ( "button" ) ;
98+ deleteButton . className = "btn btn-warning" ;
99+ deleteButton . innerText = "Delete" ;
100+
101+ deleteButton . addEventListener ( "click" , ( ) => {
102+ showMessage ( `Book "${ book . title } " deleted!` ) ;
103+ myLibrary . splice ( index , 1 ) ;
98104 render ( ) ;
99105 } ) ;
100- }
106+
107+ deleteCell . appendChild ( deleteButton ) ;
108+ } ) ;
101109}
110+
0 commit comments