@@ -27,12 +27,17 @@ const pagesInput = document.getElementById("pages");
2727const readCheckbox = document . getElementById ( "check" ) ;
2828
2929function submit ( ) {
30- if ( title . value == "" || author . value == "" || pages . value == "" ) {
31- alert ( "Please fill all fields!" ) ;
32- return false ;
30+ const title = titleInput . value . trim ( ) ;
31+ const author = authorInput . value . trim ( ) ;
32+ const pages = Number ( pagesInput . value ) ;
33+
34+ if ( ! title || ! author || ! pages || pages <= 0 ) {
35+ alert ( "Please fill all fields correctly!" ) ;
36+ return ;
3337 }
3438
35- let book = new Book ( title . value , author . value , pages . value , check . checked ) ;
39+ const book = new Book ( title , author , pages , readCheckbox . checked ) ;
40+
3641 myLibrary . push ( book ) ;
3742 render ( ) ;
3843}
@@ -45,41 +50,50 @@ function Book(title, author, pages, check) {
4550}
4651
4752function render ( ) {
48- let table = document . getElementById ( "display" ) ;
49- let rowsNumber = table . rows . length ;
50- //delete old table
51- for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) {
52- table . deleteRow ( n ) ;
53+ const table = document . getElementById ( "display" ) ;
54+
55+ // remove old rows (keep header row)
56+ while ( table . rows . length > 1 ) {
57+ table . deleteRow ( 1 ) ;
5358 }
5459
55- //add and wait for action for read/unread button
56- let changeBut = document . createElement ( "button" ) ;
57- changeBut . id = i ;
58- changeBut . className = "btn btn-success" ;
59- wasReadCell . appendChild ( changeBut ) ;
60- let readStatus = "" ;
61- if ( myLibrary [ i ] . check == true ) {
62- readStatus = "Yes" ;
63- } else {
64- readStatus = "No" ;
65- }
66- changeBut . innerText = readStatus ;
67-
68- changeBut . addEventListener ( "click" , function ( ) {
69- myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
70- render ( ) ;
71- } ) ;
72-
73- //add delete button to every row and render again
74- let delButton = document . createElement ( "button" ) ;
75- delButton . id = i + 5 ;
76- deleteCell . appendChild ( delButton ) ;
77- delButton . className = "btn btn-warning" ;
78- delButton . innerHTML = "Delete" ;
79- delButton . addEventListener ( "click" , function ( ) {
80- alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
60+ for ( let i = 0 ; i < myLibrary . length ; i ++ ) {
61+ const row = table . insertRow ( 1 ) ;
62+
63+ const titleCell = row . insertCell ( 0 ) ;
64+ const authorCell = row . insertCell ( 1 ) ;
65+ const pagesCell = row . insertCell ( 2 ) ;
66+ const wasReadCell = row . insertCell ( 3 ) ;
67+ const deleteCell = row . insertCell ( 4 ) ;
68+
69+ titleCell . textContent = myLibrary [ i ] . title ;
70+ authorCell . textContent = myLibrary [ i ] . author ;
71+ pagesCell . textContent = myLibrary [ i ] . pages ;
72+
73+ const toggleButton = document . createElement ( "button" ) ;
74+ toggleButton . className = "btn btn-success" ;
75+ toggleButton . textContent = myLibrary [ i ] . check ? "Yes" : "No" ;
76+
77+ toggleButton . addEventListener ( "click" , function ( ) {
78+ myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
79+ render ( ) ;
80+ } ) ;
81+
82+ wasReadCell . appendChild ( toggleButton ) ;
83+
84+ const deleteButton = document . createElement ( "button" ) ;
85+ deleteButton . className = "btn btn-warning" ;
86+ deleteButton . textContent = "Delete" ;
87+
88+ deleteButton . addEventListener ( "click" , function ( ) {
89+ const deletedTitle = myLibrary [ i ] . title ;
90+
8191 myLibrary . splice ( i , 1 ) ;
8292 render ( ) ;
93+
94+ alert ( `You've deleted title: ${ deletedTitle } ` ) ;
8395 } ) ;
96+
97+ deleteCell . appendChild ( deleteButton ) ;
8498 }
8599}
0 commit comments