11let myLibrary = [ ] ;
22
3- window . addEventListener ( "load" , function ( e ) {
3+ const titleInputEl = document . getElementById ( "title" ) ;
4+ const authorInputEl = document . getElementById ( "author" ) ;
5+ const pagesInputEl = document . getElementById ( "pages" ) ;
6+ const isReadInputEl = document . getElementById ( "check" ) ;
7+ const submitBookBtnEl = document . getElementById ( "submit-book-btn" ) ;
8+ const displayTableEl = document . getElementById ( "display" ) ;
9+
10+ window . addEventListener ( "load" , function ( ) {
411 populateStorage ( ) ;
512 render ( ) ;
613} ) ;
714
15+ submitBookBtnEl . addEventListener ( "click" , submit ) ;
16+
817function populateStorage ( ) {
9- if ( myLibrary . length == 0 ) {
10- let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , " 252" , true ) ;
18+ if ( myLibrary . length === 0 ) {
19+ let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , 252 , true ) ;
1120 let book2 = new Book (
1221 "The Old Man and the Sea" ,
1322 "Ernest Hemingway" ,
14- " 127" ,
23+ 127 ,
1524 true
1625 ) ;
1726 myLibrary . push ( book1 ) ;
1827 myLibrary . push ( book2 ) ;
19- render ( ) ;
2028 }
2129}
2230
23- const title = document . getElementById ( "title" ) ;
24- const author = document . getElementById ( "author" ) ;
25- const pages = document . getElementById ( "pages" ) ;
26- const check = document . getElementById ( "check" ) ;
27-
2831//check the right input from forms and if its ok -> add the new book (object in array)
2932//via Book function and start render function
3033function submit ( ) {
34+ const normalizedTitle = titleInputEl . value . trim ( ) ;
35+ const normalizedAuthor = authorInputEl . value . trim ( ) ;
36+ const pagesCount = Number ( pagesInputEl . value ) ;
37+
3138 if (
32- title . value == null ||
33- title . value == "" ||
34- pages . value == null ||
35- pages . value == ""
39+ normalizedTitle === "" ||
40+ normalizedAuthor = == "" ||
41+ ! Number . isInteger ( pagesCount ) ||
42+ pagesCount <= 0
3643 ) {
37- alert ( "Please fill all fields!" ) ;
44+ alert (
45+ "Please fill all fields correctly. Title and author are required, and page count must be a positive whole number."
46+ ) ;
3847 return false ;
3948 } else {
40- let book = new Book ( title . value , title . value , pages . value , check . checked ) ;
41- library . push ( book ) ;
49+ let book = new Book (
50+ normalizedTitle ,
51+ normalizedAuthor ,
52+ pagesCount ,
53+ isReadInputEl . checked
54+ ) ;
55+ myLibrary . push ( book ) ;
4256 render ( ) ;
57+
58+ titleInputEl . value = "" ;
59+ authorInputEl . value = "" ;
60+ pagesInputEl . value = "" ;
61+ isReadInputEl . checked = false ;
4362 }
4463}
4564
@@ -51,53 +70,43 @@ function Book(title, author, pages, check) {
5170}
5271
5372function render ( ) {
54- let table = document . getElementById ( "display" ) ;
55- let rowsNumber = table . rows . length ;
56- //delete old table
57- for ( let n = rowsNumber - 1 ; n > 0 ; n -- {
58- table . deleteRow ( n ) ;
59- }
73+ const tableBodyEl = displayTableEl . tBodies [ 0 ] ;
74+ tableBodyEl . replaceChildren ( ) ;
75+
6076 //insert updated row and cells
6177 let length = myLibrary . length ;
6278 for ( let i = 0 ; i < length ; i ++ ) {
63- let row = table . insertRow ( 1 ) ;
79+ let row = tableBodyEl . insertRow ( ) ;
6480 let titleCell = row . insertCell ( 0 ) ;
6581 let authorCell = row . insertCell ( 1 ) ;
6682 let pagesCell = row . insertCell ( 2 ) ;
6783 let wasReadCell = row . insertCell ( 3 ) ;
6884 let deleteCell = row . insertCell ( 4 ) ;
69- titleCell . innerHTML = myLibrary [ i ] . title ;
70- authorCell . innerHTML = myLibrary [ i ] . author ;
71- pagesCell . innerHTML = myLibrary [ i ] . pages ;
85+ titleCell . textContent = myLibrary [ i ] . title ;
86+ authorCell . textContent = myLibrary [ i ] . author ;
87+ pagesCell . textContent = String ( myLibrary [ i ] . pages ) ;
7288
7389 //add and wait for action for read/unread button
74- let changeBut = document . createElement ( "button" ) ;
75- changeBut . id = i ;
76- changeBut . className = "btn btn-success" ;
77- wasReadCell . appendChild ( changeBut ) ;
78- let readStatus = "" ;
79- if ( myLibrary [ i ] . check == false ) {
80- readStatus = "Yes" ;
81- } else {
82- readStatus = "No" ;
83- }
84- changeBut . innerText = readStatus ;
90+ let toggleReadButton = document . createElement ( "button" ) ;
91+ toggleReadButton . className = "btn btn-success" ;
92+ wasReadCell . appendChild ( toggleReadButton ) ;
93+ toggleReadButton . textContent = myLibrary [ i ] . check === false ? "No" : "Yes" ;
8594
86- changeBut . addEventListener ( "click" , function ( ) {
95+ toggleReadButton . addEventListener ( "click" , function ( ) {
8796 myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
8897 render ( ) ;
8998 } ) ;
9099
91100 //add delete button to every row and render again
92- let delButton = document . createElement ( "button" ) ;
93- delBut . id = i + 5 ;
94- deleteCell . appendChild ( delBut ) ;
95- delBut . className = "btn btn-warning" ;
96- delBut . innerHTML = "Delete" ;
97- delBut . addEventListener ( "clicks" , function ( ) {
98- alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
101+ let deleteButton = document . createElement ( "button" ) ;
102+ deleteCell . appendChild ( deleteButton ) ;
103+ deleteButton . className = "btn btn-warning" ;
104+ deleteButton . textContent = "Delete" ;
105+ deleteButton . addEventListener ( "click" , function ( ) {
106+ const deletedTitle = myLibrary [ i ] . title ;
99107 myLibrary . splice ( i , 1 ) ;
100108 render ( ) ;
109+ alert ( `You've deleted title: ${ deletedTitle } ` ) ;
101110 } ) ;
102111 }
103112}
0 commit comments