@@ -7,16 +7,15 @@ 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" ,
14- " 127" ,
14+ 127 ,
1515 true
1616 ) ;
1717 myLibrary . push ( book1 ) ;
1818 myLibrary . push ( book2 ) ;
19- render ( ) ;
2019 }
2120}
2221
@@ -25,36 +24,39 @@ const author = document.getElementById("author");
2524const pages = document . getElementById ( "pages" ) ;
2625const check = document . getElementById ( "check" ) ;
2726
28- //check the right input from forms and if its ok -> add the new book (object in array)
29- //via Book function and start render function
3027function submit ( ) {
31- if (
32- title . value == null ||
33- title . value == "" ||
34- pages . value == null ||
35- pages . value == ""
36- ) {
28+ const titleValue = title . value . trim ( ) ;
29+ const authorValue = author . value . trim ( ) ;
30+ const pagesValue = pages . value . trim ( ) ;
31+ const pagesNumber = Number ( pagesValue ) ;
32+
33+ if ( ! titleValue || ! authorValue || ! pagesValue ) {
3734 alert ( "Please fill all fields!" ) ;
38- return false ;
39- } else {
40- const alreadyExists = myLibrary . some (
41- ( book ) => book . title === title . value ) ;
42- if ( alreadyExists ) {
43- alert ( "This book is already in your library!" ) ;
44- return ;
45- }
35+ return ;
4636 }
47- let book = new Book ( title . value , author . value , pages . value , check . checked ) ;
48- myLibrary . push ( book ) ;
49- render ( ) ;
50-
51- //clear the form after submit
52- title . value = "" ;
53- author . value = "" ;
54- pages . value = "" ;
55- check . checked = false ;
37+
38+ if ( ! Number . isFinite ( pagesNumber ) || pagesNumber <= 0 ) {
39+ alert ( "Pages must be a positive number." ) ;
40+ return ;
5641 }
5742
43+ const alreadyExists = myLibrary . some (
44+ ( book ) => book . title . toLowerCase ( ) === titleValue . toLowerCase ( )
45+ ) ;
46+ if ( alreadyExists ) {
47+ alert ( "This book is already in your library!" ) ;
48+ return ;
49+ }
50+
51+ let book = new Book ( titleValue , authorValue , pagesNumber , check . checked ) ;
52+ myLibrary . push ( book ) ;
53+ render ( ) ;
54+
55+ title . value = "" ;
56+ author . value = "" ;
57+ pages . value = "" ;
58+ check . checked = false ;
59+ }
5860
5961function Book ( title , author , pages , check ) {
6062 this . title = title ;
@@ -64,53 +66,44 @@ function Book(title, author, pages, check) {
6466}
6567
6668function render ( ) {
67- let table = document . getElementById ( "display" ) ;
68- let rowsNumber = table . rows . length ;
69- //delete old table
70- for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) {
71- table . deleteRow ( n ) ;
72- }
73- //insert updated row and cells
74- let length = myLibrary . length ;
75- for ( let i = 0 ; i < length ; i ++ ) {
76- let row = table . insertRow ( 1 ) ;
77- let titleCell = row . insertCell ( 0 ) ;
78- let authorCell = row . insertCell ( 1 ) ;
79- let pagesCell = row . insertCell ( 2 ) ;
80- let wasReadCell = row . insertCell ( 3 ) ;
81- let deleteCell = row . insertCell ( 4 ) ;
82- titleCell . innerHTML = myLibrary [ i ] . title ;
83- authorCell . innerHTML = myLibrary [ i ] . author ;
84- pagesCell . innerHTML = myLibrary [ i ] . pages ;
85-
86- //add and wait for action for read/unread button
87- let changeBut = document . createElement ( "button" ) ;
88- changeBut . id = i ;
89- changeBut . className = "btn btn-success" ;
90- wasReadCell . appendChild ( changeBut ) ;
91- let readStatus = "" ;
92- if ( myLibrary [ i ] . check == false ) {
93- readStatus = "No" ;
94- } else {
95- readStatus = "Yes" ;
96- }
97- changeBut . innerText = readStatus ;
69+ const tbody = document . getElementById ( "bookRows" ) ;
70+ tbody . textContent = "" ;
9871
72+ for ( let i = 0 ; i < myLibrary . length ; i ++ ) {
73+ const book = myLibrary [ i ] ;
74+
75+ const row = tbody . insertRow ( ) ;
76+
77+ const titleCell = row . insertCell ( 0 ) ;
78+ const authorCell = row . insertCell ( 1 ) ;
79+ const pagesCell = row . insertCell ( 2 ) ;
80+ const wasReadCell = row . insertCell ( 3 ) ;
81+ const deleteCell = row . insertCell ( 4 ) ;
82+
83+ titleCell . textContent = book . title ;
84+ authorCell . textContent = book . author ;
85+ pagesCell . textContent = book . pages ;
86+
87+ const changeBut = document . createElement ( "button" ) ;
88+ changeBut . className = "btn btn-success" ;
89+ changeBut . textContent = book . check ? "Yes" : "No" ;
9990 changeBut . addEventListener ( "click" , function ( ) {
100- myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
91+ book . check = ! book . check ;
10192 render ( ) ;
10293 } ) ;
94+ wasReadCell . appendChild ( changeBut ) ;
10395
104- //add delete button to every row and render again
10596 let delBut = document . createElement ( "button" ) ;
106- delBut . id = i + 5 ;
107- deleteCell . appendChild ( delBut ) ;
10897 delBut . className = "btn btn-warning" ;
109- delBut . innerHTML = "Delete" ;
98+ delBut . textContent = "Delete" ;
11099 delBut . addEventListener ( "click" , function ( ) {
111- alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
112- myLibrary . splice ( i , 1 ) ;
100+ const bookIndex = myLibrary . indexOf ( book ) ;
101+ if ( bookIndex === - 1 ) return ;
102+
103+ myLibrary . splice ( bookIndex , 1 ) ;
113104 render ( ) ;
105+ alert ( `You've deleted title: ${ book . title } ` ) ;
114106 } ) ;
107+ deleteCell . appendChild ( delBut ) ;
115108 }
116109}
0 commit comments