@@ -7,7 +7,7 @@ window.addEventListener("load", function (e) {
77
88function populateStorage ( ) {
99 if ( myLibrary . length == 0 ) {
10- let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , "252" , true ) ;
10+ let book1 = new Book ( "Robinson Crusoe" , "Daniel Defoe" , "252" , true ) ;
1111 let book2 = new Book (
1212 "The Old Man and the Sea" ,
1313 "Ernest Hemingway" ,
@@ -28,21 +28,56 @@ const check = document.getElementById("check");
2828//check the right input from forms and if its ok -> add the new book (object in array)
2929//via Book function and start render function
3030function submit ( ) {
31+ // Letters + spaces only (authors)
32+ const lettersOnly = / ^ [ A - Z a - z \s ] + $ / ;
33+
34+ // Title: letters + numbers + spaces (no special characters)
35+ const titleAllowed = / ^ [ A - Z a - z 0 - 9 \s ] + $ / ;
36+
37+ // Empty field check
3138 if (
32- title . value == null ||
33- title . value == "" ||
34- pages . value == null ||
35- pages . value == ""
39+ title . value . trim ( ) === "" ||
40+ author . value . trim ( ) === "" ||
41+ pages . value . trim ( ) === ""
3642 ) {
3743 alert ( "Please fill all fields!" ) ;
3844 return false ;
39- } else {
40- let book = new Book ( title . value , title . value , pages . value , check . checked ) ;
41- library . push ( book ) ;
42- render ( ) ;
4345 }
46+
47+ // Title validation
48+ if ( ! titleAllowed . test ( title . value ) ) {
49+ alert ( "Title must contain only letters, numbers, and spaces!" ) ;
50+ return false ;
51+ }
52+
53+ // Author validation
54+ if ( ! lettersOnly . test ( author . value ) ) {
55+ alert ( "Author name must contain only letters!" ) ;
56+ return false ;
57+ }
58+
59+ // Pages >= 1
60+ let pagesNum = Number ( pages . value ) ;
61+
62+ if ( isNaN ( pagesNum ) || pagesNum < 1 ) {
63+ alert ( "Pages must be a number greater than or equal to 1!" ) ;
64+ return false ;
65+ }
66+
67+ // Create and save the book
68+ let book = new Book ( title . value , author . value , pages . value , check . checked ) ;
69+ myLibrary . push ( book ) ;
70+
71+ // Clear inputs (optional)
72+ title . value = "" ;
73+ author . value = "" ;
74+ pages . value = "" ;
75+ check . checked = false ;
76+
77+ render ( ) ;
4478}
4579
80+
4681function Book ( title , author , pages , check ) {
4782 this . title = title ;
4883 this . author = author ;
@@ -53,8 +88,8 @@ function Book(title, author, pages, check) {
5388function render ( ) {
5489 let table = document . getElementById ( "display" ) ;
5590 let rowsNumber = table . rows . length ;
56- //delete old table
57- for ( let n = rowsNumber - 1 ; n > 0 ; n -- {
91+ // delete old table rows (keep header row 0)
92+ for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) {
5893 table . deleteRow ( n ) ;
5994 }
6095 //insert updated row and cells
@@ -72,11 +107,12 @@ function render() {
72107
73108 //add and wait for action for read/unread button
74109 let changeBut = document . createElement ( "button" ) ;
75- changeBut . id = i ;
110+ changeBut . id = "read-btn-" + i ;
76111 changeBut . className = "btn btn-success" ;
77112 wasReadCell . appendChild ( changeBut ) ;
78113 let readStatus = "" ;
79- if ( myLibrary [ i ] . check == false ) {
114+ // show "Yes" when check is true (read), otherwise "No"
115+ if ( myLibrary [ i ] . check === true ) {
80116 readStatus = "Yes" ;
81117 } else {
82118 readStatus = "No" ;
@@ -89,15 +125,19 @@ function render() {
89125 } ) ;
90126
91127 //add delete button to every row and render again
92- let delButton = document . createElement ( "button" ) ;
93- delBut . id = i + 5 ;
94- deleteCell . appendChild ( delBut ) ;
128+ let delBut = document . createElement ( "button" ) ;
129+ delBut . id = "del-btn-" + i ;
95130 delBut . className = "btn btn-warning" ;
96131 delBut . innerHTML = "Delete" ;
97- delBut . addEventListener ( "clicks" , function ( ) {
132+ deleteCell . appendChild ( delBut ) ;
133+
134+ delBut . addEventListener ( "click" , function ( ) {
98135 alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
99136 myLibrary . splice ( i , 1 ) ;
100137 render ( ) ;
101138 } ) ;
102139 }
103140}
141+
142+
143+ // Book Library bugs fixed and Book Library working normal.
0 commit comments