1+
2+
13let myLibrary = [ ] ;
24
35window . addEventListener ( "load" , function ( e ) {
@@ -7,16 +9,15 @@ window.addEventListener("load", function (e) {
79
810function populateStorage ( ) {
911 if ( myLibrary . length == 0 ) {
10- let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , " 252" , true ) ;
12+ let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , 252 , true ) ;
1113 let book2 = new Book (
1214 "The Old Man and the Sea" ,
1315 "Ernest Hemingway" ,
14- " 127" ,
16+ 127 ,
1517 true
1618 ) ;
1719 myLibrary . push ( book1 ) ;
1820 myLibrary . push ( book2 ) ;
19- render ( ) ;
2021 }
2122}
2223
@@ -28,17 +29,19 @@ const check = document.getElementById("check");
2829//check the right input from forms and if its ok -> add the new book (object in array)
2930//via Book function and start render function
3031function submit ( ) {
31- if (
32- title . value == null ||
33- title . value == "" ||
34- pages . value == null ||
35- pages . value == ""
36- ) {
37- alert ( "Please fill all fields!" ) ;
32+ const cleanTitle = title . value . trim ( ) ;
33+ const cleanAuthor = author . value . trim ( ) ;
34+ const cleanPages = parseInt ( pages . value , 10 ) ;
35+
36+ if ( ! cleanTitle || ! cleanAuthor ) { // title and author doesn't allowed to contain only space characters
37+ alert ( "Title and Author cannot be empty or spaces only." ) ;
38+ return false ;
39+ } else if ( isNaN ( cleanPages ) || cleanPages <= 0 ) { // the value of type of pages must be a integer
40+ alert ( "Pages must be a positive whole number." ) ;
3841 return false ;
3942 } else {
40- let book = new Book ( title . value , title . value , pages . value , check . checked ) ;
41- library . push ( book ) ;
43+ let book = new Book ( cleanTitle , cleanAuthor , pages . value , check . checked ) ; //// title and author be allowed to contain leading or trailing space characters, we storage using parseInt
44+ myLibrary . push ( book ) ;
4245 render ( ) ;
4346 }
4447}
@@ -52,11 +55,10 @@ function Book(title, author, pages, check) {
5255
5356function render ( ) {
5457 let table = document . getElementById ( "display" ) ;
55- let rowsNumber = table . rows . length ;
5658 //delete old table
57- for ( let n = rowsNumber - 1 ; n > 0 ; n -- {
58- table . deleteRow ( n ) ;
59- }
59+ // Keep only the header row
60+ table . innerHTML = table . rows [ 0 ] . outerHTML ;
61+
6062 //insert updated row and cells
6163 let length = myLibrary . length ;
6264 for ( let i = 0 ; i < length ; i ++ ) {
@@ -66,37 +68,35 @@ function render() {
6668 let pagesCell = row . insertCell ( 2 ) ;
6769 let wasReadCell = row . insertCell ( 3 ) ;
6870 let deleteCell = row . insertCell ( 4 ) ;
69- titleCell . innerHTML = myLibrary [ i ] . title ;
70- authorCell . innerHTML = myLibrary [ i ] . author ;
71- pagesCell . innerHTML = myLibrary [ i ] . pages ;
71+ titleCell . textContent = myLibrary [ i ] . title ;
72+ authorCell . textContent = myLibrary [ i ] . author ;
73+ pagesCell . textContent = myLibrary [ i ] . pages ;
7274
7375 //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 ) ;
76+ let toggleReadButton = document . createElement ( "button" ) ;
77+ toggleReadButton . className = "btn btn-success" ;
78+ wasReadCell . appendChild ( toggleReadButton ) ;
7879 let readStatus = "" ;
79- if ( myLibrary [ i ] . check == false ) {
80- readStatus = "Yes" ;
81- } else {
82- readStatus = "No" ;
83- }
84- changeBut . innerText = readStatus ;
8580
86- changeBut . addEventListener ( "click" , function ( ) {
81+ readStatus = myLibrary [ i ] . check ? "Yes" : "No" ;
82+
83+ toggleReadButton . innerText = readStatus ;
84+
85+ toggleReadButton . addEventListener ( "click" , function ( ) {
8786 myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
8887 render ( ) ;
8988 } ) ;
89+
9090
9191 //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 } ` ) ;
99- myLibrary . splice ( i , 1 ) ;
92+ let deleteBookButton = document . createElement ( "button" ) ;
93+ deleteCell . appendChild ( deleteBookButton ) ;
94+ deleteBookButton . className = "btn btn-warning" ;
95+ deleteBookButton . innerHTML = "Delete " ;
96+ deleteBookButton . addEventListener ( "click" , function ( ) {
97+ const deleteTitle = myLibrary [ i ] . title ; //store first
98+ myLibrary . splice ( i , 1 ) ; // delete it
99+ alert ( `You have deleted title: ${ deleteTitle } ` ) ;
100100 render ( ) ;
101101 } ) ;
102102 }
0 commit comments