1- let myLibrary = [ ] ;
1+ const myLibrary = [ ] ;
22
33window . addEventListener ( "load" , function ( e ) {
44 populateStorage ( ) ;
@@ -7,11 +7,11 @@ window.addEventListener("load", function (e) {
77
88function populateStorage ( ) {
99 if ( myLibrary . length == 0 ) {
10- let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , "252" , true ) ;
11- let book2 = new Book (
10+ const book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , "252" , true ) ;
11+ 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 ) ;
@@ -20,25 +20,34 @@ function populateStorage() {
2020 }
2121}
2222
23- const title = document . getElementById ( "title" ) ;
24- const author = document . getElementById ( "author" ) ;
25- const pages = document . getElementById ( "pages" ) ;
26- const check = document . getElementById ( "check" ) ;
23+ const titleInput = document . getElementById ( "title" ) ;
24+ const authorInput = document . getElementById ( "author" ) ;
25+ const pagesInput = document . getElementById ( "pages" ) ;
26+ const readCheckBox = document . getElementById ( "check" ) ;
2727
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- if (
32- title . value == null ||
33- title . value == "" ||
34- pages . value == null ||
35- pages . value == ""
36- ) {
31+ const trimmedTitle = titleInput . value . trim ( ) ;
32+ const trimmedAuthor = authorInput . value . trim ( ) ;
33+ const trimmedPages = + pagesInput . value . trim ( ) ;
34+
35+ if ( ! trimmedTitle || ! trimmedAuthor || ! trimmedPages ) {
3736 alert ( "Please fill all fields!" ) ;
3837 return false ;
3938 } else {
40- let book = new Book ( title . value , title . value , pages . value , check . checked ) ;
41- library . push ( book ) ;
39+ let book = new Book (
40+ trimmedTitle ,
41+ trimmedAuthor ,
42+ trimmedPages ,
43+ readCheckbox . checked
44+ ) ;
45+ myLibrary . push ( book ) ;
46+
47+ titleInput . value = "" ;
48+ authorInput . value = "" ;
49+ pagesInput . value = "" ;
50+ readCheckbox . checked = false ;
4251 render ( ) ;
4352 }
4453}
@@ -51,53 +60,48 @@ function Book(title, author, pages, check) {
5160}
5261
5362function render ( ) {
54- let table = document . getElementById ( "display" ) ;
55- let rowsNumber = table . rows . length ;
63+ const table = document . getElementById ( "display" ) ;
64+ const rowsNumber = table . rows . length ;
5665 //delete old table
57- for ( let n = rowsNumber - 1 ; n > 0 ; n -- {
66+ for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) {
5867 table . deleteRow ( n ) ;
5968 }
6069 //insert updated row and cells
61- let length = myLibrary . length ;
70+ const length = myLibrary . length ;
6271 for ( let i = 0 ; i < length ; i ++ ) {
63- let row = table . insertRow ( 1 ) ;
64- let titleCell = row . insertCell ( 0 ) ;
65- let authorCell = row . insertCell ( 1 ) ;
66- let pagesCell = row . insertCell ( 2 ) ;
67- let wasReadCell = row . insertCell ( 3 ) ;
68- let deleteCell = row . insertCell ( 4 ) ;
69- titleCell . innerHTML = myLibrary [ i ] . title ;
70- authorCell . innerHTML = myLibrary [ i ] . author ;
71- pagesCell . innerHTML = myLibrary [ i ] . pages ;
72+ const row = table . insertRow ( 1 ) ;
73+ const titleCell = row . insertCell ( 0 ) ;
74+ const authorCell = row . insertCell ( 1 ) ;
75+ const pagesCell = row . insertCell ( 2 ) ;
76+ const wasReadCell = row . insertCell ( 3 ) ;
77+ const deleteCell = row . insertCell ( 4 ) ;
78+ titleCell . textContent = myLibrary [ i ] . title ;
79+ authorCell . textContent = myLibrary [ i ] . author ;
80+ pagesCell . textContent = myLibrary [ i ] . pages ;
7281
7382 //add and wait for action for read/unread button
74- let changeBut = document . createElement ( "button" ) ;
83+ const changeBut = document . createElement ( "button" ) ;
7584 changeBut . id = i ;
7685 changeBut . className = "btn btn-success" ;
7786 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 ;
87+ changeBut . innerHTML = myLibrary [ i ] . check ? "Yes" : "No" ;
8588
8689 changeBut . addEventListener ( "click" , function ( ) {
8790 myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
8891 render ( ) ;
8992 } ) ;
9093
9194 //add delete button to every row and render again
92- let delButton = document . createElement ( "button" ) ;
95+ const delButton = document . createElement ( "button" ) ;
9396 delBut . id = i + 5 ;
9497 deleteCell . appendChild ( delBut ) ;
9598 delBut . className = "btn btn-warning" ;
9699 delBut . innerHTML = "Delete" ;
97- delBut . addEventListener ( "clicks " , function ( ) {
98- alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
100+ delBut . addEventListener ( "click " , function ( ) {
101+ const deletedBook = myLibrary [ i ] ;
99102 myLibrary . splice ( i , 1 ) ;
100103 render ( ) ;
104+ alert ( `You've deleted title: ${ deletedBook . title } ` ) ;
101105 } ) ;
102106 }
103107}
0 commit comments