@@ -11,7 +11,7 @@ function populateStorage() {
1111 const book2 = new Book (
1212 "The Old Man and the Sea" ,
1313 "Ernest Hemingway" ,
14- 127 ,
14+ " 127" ,
1515 true
1616 ) ;
1717 myLibrary . push ( book1 ) ;
@@ -32,16 +32,16 @@ function Book(title, author, pages, check) {
3232function submit ( ) {
3333 const trimmedTitle = titleInput . value . trim ( ) ;
3434 const trimmedAuthor = authorInput . value . trim ( ) ;
35- const trimmedPages = + pagesInput . value . trim ( ) ;
35+ const trimmedPageNumbers = + pagesInput . value . trim ( ) ;
3636
37- if ( ! trimmedTitle || ! trimmedAuthor || ! trimmedPages ) {
38- alert ( "Please fill all fields!" ) ;
37+ if ( ! trimmedTitle || ! trimmedAuthor || ! Number . isInteger ( trimmedPageNumbers ) || trimmedPageNumbers <= 0 ) {
38+ alert ( "Please fill all fields! Page count must be a positive integer. " ) ;
3939 return false ;
4040 } else {
4141 let book = new Book (
4242 trimmedTitle ,
4343 trimmedAuthor ,
44- trimmedPages ,
44+ trimmedPageNumbers ,
4545 readCheckbox . checked
4646 ) ;
4747 myLibrary . push ( book ) ;
@@ -63,11 +63,9 @@ function Book(title, author, pages, check) {
6363
6464function render ( ) {
6565 const table = document . getElementById ( "display" ) ;
66- const rowsNumber = table . rows . length ;
67- //delete old table
68- for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) {
69- table . deleteRow ( n ) ;
70- }
66+ const tbody = table . querySelector ( "tbody" ) ;
67+ //clear tbody
68+ tbody . innerHTML = "" ;
7169 //insert updated row and cells
7270 const length = myLibrary . length ;
7371 for ( let i = 0 ; i < length ; i ++ ) {
@@ -83,7 +81,6 @@ function render() {
8381
8482 //add and wait for action for read/unread button
8583 const changeBut = document . createElement ( "button" ) ;
86- changeBut . id = i ;
8784 changeBut . className = "btn btn-success" ;
8885 wasReadCell . appendChild ( changeBut ) ;
8986 changeBut . innerHTML = myLibrary [ i ] . check ? "Yes" : "No" ;
@@ -94,8 +91,7 @@ function render() {
9491 } ) ;
9592
9693 //add delete button to every row and render again
97- const delButton = document . createElement ( "button" ) ;
98- delBut . id = i + 5 ;
94+ const delBut = document . createElement ( "button" ) ;
9995 deleteCell . appendChild ( delBut ) ;
10096 delBut . className = "btn btn-warning" ;
10197 delBut . innerHTML = "Delete" ;
0 commit comments