@@ -6,14 +6,9 @@ window.addEventListener("load", function (e) {
66} ) ;
77
88function populateStorage ( ) {
9- if ( myLibrary . length == 0 ) {
10- let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , "252" , true ) ;
11- let book2 = new Book (
12- "The Old Man and the Sea" ,
13- "Ernest Hemingway" ,
14- "127" ,
15- true
16- ) ;
9+ if ( myLibrary . length === 0 ) {
10+ let book1 = new Book ( "Robinson Crusoe" , "Daniel Defoe" , 252 , true ) ;
11+ let book2 = new Book ( "The Old Man and the Sea" , "Ernest Hemingway" , 127 , true ) ;
1712 myLibrary . push ( book1 ) ;
1813 myLibrary . push ( book2 ) ;
1914 render ( ) ;
@@ -25,21 +20,21 @@ const author = document.getElementById("author");
2520const pages = document . getElementById ( "pages" ) ;
2621const check = document . getElementById ( "check" ) ;
2722
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
3023function submit ( ) {
3124 if (
32- title . value == null ||
33- title . value == "" ||
34- pages . value == null ||
35- pages . value == ""
25+ ! title . value ||
26+ ! author . value ||
27+ ! pages . value
3628 ) {
3729 alert ( "Please fill all fields!" ) ;
3830 return false ;
3931 } else {
40- let book = new Book ( title . value , title . value , pages . value , check . checked ) ;
41- library . push ( book ) ;
32+ let book = new Book ( title . value , author . value , pages . value , check . checked ) ;
33+ myLibrary . push ( book ) ;
4234 render ( ) ;
35+ // Reset form and collapse
36+ document . querySelector ( "form" ) . reset ( ) ;
37+ $ ( "#demo" ) . collapse ( "hide" ) ;
4338 }
4439}
4540
@@ -53,14 +48,13 @@ function Book(title, author, pages, check) {
5348function render ( ) {
5449 let table = document . getElementById ( "display" ) ;
5550 let rowsNumber = table . rows . length ;
56- //delete old table
57- for ( let n = rowsNumber - 1 ; n > 0 ; n -- {
51+ // Delete old table rows (except header)
52+ for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) {
5853 table . deleteRow ( n ) ;
5954 }
60- //insert updated row and cells
61- let length = myLibrary . length ;
62- for ( let i = 0 ; i < length ; i ++ ) {
63- let row = table . insertRow ( 1 ) ;
55+ // Insert updated rows
56+ for ( let i = 0 ; i < myLibrary . length ; i ++ ) {
57+ let row = table . insertRow ( - 1 ) ; // Append at the end of tbody
6458 let titleCell = row . insertCell ( 0 ) ;
6559 let authorCell = row . insertCell ( 1 ) ;
6660 let pagesCell = row . insertCell ( 2 ) ;
@@ -70,34 +64,27 @@ function render() {
7064 authorCell . innerHTML = myLibrary [ i ] . author ;
7165 pagesCell . innerHTML = myLibrary [ i ] . pages ;
7266
73- //add and wait for action for read/unread button
67+ // Read/Unread button
7468 let changeBut = document . createElement ( "button" ) ;
75- changeBut . id = i ;
69+ changeBut . id = `read- ${ i } ` ;
7670 changeBut . className = "btn btn-success" ;
7771 wasReadCell . appendChild ( changeBut ) ;
78- let readStatus = "" ;
79- if ( myLibrary [ i ] . check == false ) {
80- readStatus = "Yes" ;
81- } else {
82- readStatus = "No" ;
83- }
84- changeBut . innerText = readStatus ;
85-
72+ changeBut . innerText = myLibrary [ i ] . check ? "Yes" : "No" ;
8673 changeBut . addEventListener ( "click" , function ( ) {
8774 myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
8875 render ( ) ;
8976 } ) ;
9077
91- //add delete button to every row and render again
78+ // Delete button
9279 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 ( ) {
80+ delButton . id = `delete- ${ i } ` ;
81+ delButton . className = "btn btn-warning" ;
82+ delButton . innerHTML = "Delete " ;
83+ deleteCell . appendChild ( delButton ) ;
84+ delButton . addEventListener ( "click " , function ( ) {
9885 alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
9986 myLibrary . splice ( i , 1 ) ;
10087 render ( ) ;
10188 } ) ;
10289 }
103- }
90+ }
0 commit comments