1
- let myLibrary = [ ] ;
1
+ const myLibrary = [ ] ;
2
2
3
3
window . addEventListener ( "load" , function ( e ) {
4
4
populateStorage ( ) ;
@@ -7,39 +7,42 @@ window.addEventListener("load", function (e) {
7
7
8
8
function populateStorage ( ) {
9
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
- ) ;
10
+ const book1 = new Book ( "Robinson Crusoe" , "Daniel Defoe" , "252" , true ) ;
11
+ const book2 = new Book ( "The Old Man and the Sea" , "Ernest Hemingway" , "127" , true ) ;
12
+
17
13
myLibrary . push ( book1 ) ;
18
14
myLibrary . push ( book2 ) ;
19
- render ( ) ;
15
+ // render(); // already called in 'load' event
20
16
}
21
17
}
22
18
23
- const title = document . getElementById ( "title" ) ;
24
- const author = document . getElementById ( "author" ) ;
25
- const pages = document . getElementById ( "pages" ) ;
26
- const check = document . getElementById ( "check" ) ;
27
-
28
19
//check the right input from forms and if its ok -> add the new book (object in array)
29
20
//via Book function and start render function
30
21
function submit ( ) {
22
+
23
+ const title = document . getElementById ( "title" ) ; // move form elements previously declared outside of function / in global scope
24
+ const author = document . getElementById ( "author" ) ;
25
+ const pages = document . getElementById ( "pages" ) ;
26
+ const check = document . getElementById ( "check" ) ;
27
+
31
28
if (
32
- title . value == null ||
29
+ title . value == null ||
33
30
title . value == "" ||
31
+ author . value == null || // reset all form field after submission
32
+ author . value == "" ||
34
33
pages . value == null ||
35
34
pages . value == ""
36
35
) {
37
36
alert ( "Please fill all fields!" ) ;
38
37
return false ;
39
38
} else {
40
- let book = new Book ( title . value , title . value , pages . value , check . checked ) ;
41
- library . push ( book ) ;
39
+ const book = new Book ( title . value , author . value , pages . value , check . checked ) ; // correct 'author' key
40
+ myLibrary . push ( book ) ; // correct array name
42
41
render ( ) ;
42
+ title . value = "" ;
43
+ author . value = "" ;
44
+ pages . value = "" ;
45
+ check . checked = false ;
43
46
}
44
47
}
45
48
@@ -51,36 +54,34 @@ function Book(title, author, pages, check) {
51
54
}
52
55
53
56
function render ( ) {
54
- let table = document . getElementById ( "display" ) ;
55
- let rowsNumber = table . rows . length ;
57
+ const table = document . getElementById ( "display" ) ;
58
+ const rowsNumber = table . rows . length ;
56
59
//delete old table
57
- for ( let n = rowsNumber - 1 ; n > 0 ; n -- {
60
+ for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) { // add missing closing bracket
58
61
table . deleteRow ( n ) ;
59
62
}
60
63
//insert updated row and cells
61
- let length = myLibrary . length ;
64
+ const length = myLibrary . length ; // change to 'const' for variables
62
65
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 ) ;
66
+ const row = table . insertRow ( 1 ) ;
67
+ const titleCell = row . insertCell ( 0 ) ;
68
+ const authorCell = row . insertCell ( 1 ) ;
69
+ const pagesCell = row . insertCell ( 2 ) ;
70
+ const wasReadCell = row . insertCell ( 3 ) ;
71
+ const deleteCell = row . insertCell ( 4 ) ;
69
72
titleCell . innerHTML = myLibrary [ i ] . title ;
70
73
authorCell . innerHTML = myLibrary [ i ] . author ;
71
74
pagesCell . innerHTML = myLibrary [ i ] . pages ;
72
75
73
76
//add and wait for action for read/unread button
74
- let changeBut = document . createElement ( "button" ) ;
77
+ const changeBut = document . createElement ( "button" ) ;
75
78
changeBut . id = i ;
76
79
changeBut . className = "btn btn-success" ;
77
80
wasReadCell . appendChild ( changeBut ) ;
78
81
let readStatus = "" ;
79
- if ( myLibrary [ i ] . check == false ) {
80
- readStatus = "Yes" ;
81
- } else {
82
- readStatus = "No" ;
83
- }
82
+
83
+ readStatus = myLibrary [ i ] . check ? "Yes" : "No" ; // correct read status logic for not read
84
+
84
85
changeBut . innerText = readStatus ;
85
86
86
87
changeBut . addEventListener ( "click" , function ( ) {
@@ -89,12 +90,12 @@ function render() {
89
90
} ) ;
90
91
91
92
//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 ( ) {
93
+ const delButton = document . createElement ( "button" ) ;
94
+ delButton . id = i + 5 ;
95
+ deleteCell . appendChild ( delButton ) ; // correct delButton variable name
96
+ delButton . className = "btn btn-warning" ;
97
+ delButton . innerHTML = "Delete" ;
98
+ delButton . addEventListener ( "click " , function ( ) { // click not clicks
98
99
alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
99
100
myLibrary . splice ( i , 1 ) ;
100
101
render ( ) ;
0 commit comments