1
- var title = [ "Book1" , "Book2" , "Book3" , "Book4" , "Book5" , "Book6" ] ;
2
- var author = [ "Author1" , "Author2" , "Author3" , "Author4" , "Author5" , "Author6" ] ;
3
- var lender = [ "UserC" , "UserC" , "UserD" , "UserA" , "UserA" , "UserB" ] ;
4
- var borrower = [ "UserB" , "-" , "UserC" , "-" , "-" , "UserA" ] ;
5
- var action = "-" ;
6
- var user = [ "UserA" , "UserB" , "UserC" , "UserD" ] ;
7
- var loginflag = 0 ;
8
- var username ;
9
- var message = document . getElementById ( "logged-in-user-name" ) ;
10
- message . innerHTML = "No user logged in" ;
1
+ // Write your code here!
2
+ let bookName = [ 'Book1' , 'Book2' , 'Book3' , 'Book4' , 'Book5' , 'Book6' ] ;
3
+ let author = [ 'Author1' , 'Author2' , 'Author3' , 'Author4' , 'Author5' , 'Author6' ] ;
4
+ let user = [ 'UserA' , 'UserB' , 'UserC' , 'UserD' ] ;
5
+ let lender = [ 'UserC' , 'UserC' , 'UserD' , 'UserA' , 'UserA' , 'UserB' ] ;
6
+ let borrower = [ 'UserB' , '-' , 'UserC' , '-' , '-' , 'UserA' ] ;
7
+ let action = '-' ;
11
8
12
- function addDataRow ( i ) {
13
- var tableRef = document . getElementById ( "info-table" ) ;
14
- var newRow = tableRef . insertRow ( - 1 ) ;
15
- for ( var j = 0 ; j < 6 ; j ++ ) {
16
- var newCell = newRow . insertCell ( j ) ;
17
- if ( j === 0 )
18
- newCell . innerHTML = i + 1 ;
19
- if ( j === 1 )
20
- newCell . innerHTML = title [ i ] ;
21
- if ( j === 2 )
22
- newCell . innerHTML = author [ i ] ;
23
- if ( j === 3 )
24
- newCell . innerHTML = lender [ i ] ;
25
- if ( j === 4 )
26
- newCell . innerHTML = borrower [ i ] ;
27
- if ( j === 5 )
28
- newCell . innerHTML = action ;
9
+ let buildTable = document . getElementById ( "info-table" ) ;
10
+
11
+ const tableData = ( ) => {
12
+ for ( var i = 0 ; i < 6 ; i ++ ) {
13
+ var row = buildTable . insertRow ( - 1 ) ;
14
+ row . innerHTML = `<tr>
15
+ <td>${ i + 1 } </td>
16
+ <td>${ bookName [ i ] } </td>
17
+ <td>${ author [ i ] } </td>
18
+ <td>${ lender [ i ] } </td>
19
+ <td>${ borrower [ i ] } </td>
20
+ <td>${ action } </td>
21
+ </tr>` ;
29
22
}
30
23
}
31
- for ( var i = 0 ; i < 6 ; i ++ ) {
32
- addDataRow ( i ) ;
33
- }
34
24
25
+ tableData ( ) ;
35
26
27
+ let userlogin = 0 ;
28
+ let userId ;
29
+ let userHeader = document . getElementById ( 'logged-in-user-name' ) ;
30
+ let loginInput = document . getElementById ( 'logged-user' ) ;
31
+ userHeader . innerHTML = "No user logged in" ;
36
32
37
- function changeLoggedInUser ( ) {
38
- if ( username === document . getElementById ( "logged-user" ) . value && user . indexOf ( username ) !== - 1 ) {
39
- alert ( "User has already logged in!! !" ) ;
40
- } else {
41
- username = document . getElementById ( "logged-user" ) . value ;
42
- var message = document . getElementById ( "logged-in-user-name" ) ;
43
- if ( user . indexOf ( username ) === - 1 ) {
44
- message . innerHTML = "No user logged in" ;
45
- if ( loginflag === 1 ) {
46
- var tableRef = document . getElementById ( "info-table" ) ;
47
- var rowCount = tableRef . rows . length ;
48
- tableRef . deleteRow ( rowCount - 1 ) ;
49
- loginflag = 0 ;
33
+ const changeLoggedInUser = ( ) => {
34
+ if ( userId === loginInput . value && user . indexOf ( userId ) !== - 1 ) {
35
+ alert ( "Already logged in!" ) ;
36
+ }
37
+ else {
38
+ userId = loginInput . value ;
39
+ if ( user . indexOf ( userId ) === - 1 ) {
40
+ userHeader . innerHTML = "No user logged in" ;
41
+
42
+ if ( userlogin === 1 ) {
43
+ let count = buildTable . rows . length ;
44
+ buildTable . deleteRow ( count - 1 ) ;
45
+ userlogin = 0 ;
50
46
}
51
- var tableRef = document . getElementById ( "info-table" ) ;
52
- for ( var i = 1 ; i < tableRef . rows . length ; i ++ ) {
53
- tableRef . rows [ i ] . cells [ 5 ] . innerHTML = "-" ;
47
+
48
+ for ( var i = 1 ; i < buildTable . rows . length ; i ++ ) {
49
+ buildTable . rows [ i ] . cells [ 5 ] . innerHTML = "-" ;
54
50
}
55
- } else {
56
- message . innerHTML = ` <b>Logged in user: ${ username } </b>` ;
57
- if ( loginflag === 0 ) {
58
- addBookRow ( username ) ;
59
- loginflag = 1 ;
51
+ }
52
+ else {
53
+ userHeader . innerHTML = ` <span>Logged in user: ${ userId } </span>` ;
54
+
55
+ if ( userlogin === 0 ) {
56
+ addBookTableRow ( userId ) ;
57
+ userlogin = 1 ;
60
58
}
61
- if ( loginflag === 1 ) {
62
- var tableRef = document . getElementById ( "info-table" ) ;
63
- var rowCount = tableRef . rows . length ;
64
- tableRef . deleteRow ( rowCount - 1 ) ;
65
- addBookRow ( username ) ;
59
+
60
+ if ( userlogin === 1 ) {
61
+ let count = buildTable . rows . length ;
62
+ buildTable . deleteRow ( count - 1 ) ;
63
+ addBookTableRow ( userId ) ;
66
64
}
67
- var tableRef = document . getElementById ( "info-table" ) ;
68
- for ( var i = 1 ; i < tableRef . rows . length - 1 ; i ++ ) {
69
- if ( tableRef . rows [ i ] . cells [ 4 ] . textContent === username && tableRef . rows [ i ] . cells [ 5 ] . textContent === "-" ) {
70
- tableRef . rows [ i ] . cells [ 5 ] . innerHTML = `<button id="return" onclick="returnClick(${ i } )">Return</button>` ;
71
- } else if ( tableRef . rows [ i ] . cells [ 4 ] . textContent === "-" && tableRef . rows [ i ] . cells [ 3 ] . textContent !== username ) {
72
- tableRef . rows [ i ] . cells [ 5 ] . innerHTML = `<button id="borrow" onclick="borrowClick(${ i } )">Borrow</button>` ;
73
- } else {
74
- tableRef . rows [ i ] . cells [ 5 ] . innerHTML = "-" ;
65
+
66
+ for ( var i = 1 ; i < buildTable . rows . length - 1 ; i ++ ) {
67
+ if ( buildTable . rows [ i ] . cells [ 4 ] . textContent === userId && buildTable . rows [ i ] . cells [ 5 ] . textContent === "-" ) {
68
+ buildTable . rows [ i ] . cells [ 5 ] . innerHTML = `<button id="return" onclick="returnBtn(${ i } )">Return</button>` ;
69
+ }
70
+ else if ( buildTable . rows [ i ] . cells [ 4 ] . textContent === "-" && buildTable . rows [ i ] . cells [ 3 ] . textContent !== userId ) {
71
+ buildTable . rows [ i ] . cells [ 5 ] . innerHTML = `<button id="borrow" onclick="borrowBtn(${ i } )">Borrow</button>` ;
72
+ }
73
+ else {
74
+ buildTable . rows [ i ] . cells [ 5 ] . innerHTML = "-" ;
75
75
}
76
76
}
77
77
}
78
78
}
79
79
}
80
80
81
- function addNewBook ( ) {
82
- console . log ( "daskhcahd" ) ;
83
- var titleName = document . getElementById ( "titleId" ) . value ;
84
-
85
- var authorName = document . getElementById ( "authorId" ) . value ;
86
- if ( titleName . length > 0 && authorName . length > 0 && title . indexOf ( titleName ) === - 1 ) {
87
- title . push ( titleName ) ;
88
- var tableRef = document . getElementById ( "info-table" ) ;
89
- var rowCount = tableRef . rows . length ;
90
- tableRef . deleteRow ( rowCount - 1 ) ;
91
- tableRef . insertRow ( - 1 ) . innerHTML = `<tr><td>${ tableRef . rows . length - 1 } </td>
92
- <td>${ titleName } </td>
93
- <td>${ authorName } </td>
94
- <td>${ username } </td>
95
- <td>-</td>
96
- <td>-</td>
81
+ const newBook = ( ) => {
82
+ let bookTitle = document . getElementById ( "book-name" ) . value ;
83
+ let authorName = document . getElementById ( "author-name" ) . value ;
84
+ if ( bookTitle . length > 0 && authorName . length > 0 && bookName . indexOf ( bookTitle ) === - 1 ) {
85
+ bookName . push ( bookTitle ) ;
86
+ let count = buildTable . rows . length ;
87
+ buildTable . deleteRow ( count - 1 ) ;
88
+ buildTable . insertRow ( - 1 ) . innerHTML = `<tr>
89
+ <td>${ buildTable . rows . length - 1 } </td>
90
+ <td>${ bookTitle } </td>
91
+ <td>${ authorName } </td>
92
+ <td>${ userId } </td>
93
+ <td>-</td>
94
+ <td>-</td>
97
95
</tr>` ;
98
- addBookRow ( username ) ;
99
- } else if ( title . indexOf ( titleName ) !== - 1 ) {
100
- alert ( "Book already available!! !" ) ;
96
+ addBookTableRow ( userId ) ;
97
+ } else if ( bookName . indexOf ( bookTitle ) !== - 1 ) {
98
+ alert ( "Book already exists !" ) ;
101
99
} else {
102
- if ( titleName . length === 0 && authorName . length === 0 )
103
-
104
- alert ( "Enter the author and book name!!!" ) ;
105
- else if ( authorName . length === 0 )
106
- alert ( "Enter the author name of the book!!!" ) ;
107
- else
108
- alert ( "Enter the title of Book!!!" ) ;
100
+ if ( bookTitle . length === 0 && authorName . length === 0 ) {
101
+ alert ( "Enter The Required field!" ) ;
102
+ }
103
+ else if ( authorName . length === 0 ) {
104
+ alert ( "Enter The Author Name!" ) ;
105
+ }
106
+ else if ( bookTitle . length === 0 ) {
107
+ alert ( "Enter The Book Title!" ) ;
108
+ }
109
109
}
110
110
}
111
111
112
- function addBookRow ( username ) {
113
- var tableRef = document . getElementById ( "info-table" ) ;
114
- tableRef . insertRow ( - 1 ) . innerHTML =
115
- `<tr><td>${ tableRef . rows . length - 1 } </td>
116
- <td><input type="text" id="titleId" placeholder="title"></td>
117
- <td><input type="text" id="authorId" placeholder="author"></td>
118
- <td>${ username } </td>
119
- <td>-</td>
120
- <td><button id="addbook" onclick="addNewBook()">Add book</button></td>
121
- </tr>` ;
112
+ const addBookTableRow = ( userId ) => {
113
+ buildTable . insertRow ( - 1 ) . innerHTML = `<tr>
114
+ <td>${ buildTable . rows . length - 1 } </td>
115
+ <td><input type="text" id="book-name" placeholder="Title" ></td>
116
+ <td><input type="text" id="author-name" placeholder="Author" ></td>
117
+ <td id="lender-name" >${ userId } </td>
118
+ <td>-</td>
119
+ <td><button onclick="newBook()" >Add Book</button></td>
120
+ </tr>` ;
122
121
}
123
122
124
- function returnClick ( i ) {
125
- document . getElementById ( "info-table" ) . rows [ i ] . cells [ 5 ] . innerHTML = `<button id="borrow" onclick="borrowClick (${ i } )">Borrow</button>` ;
126
- document . getElementById ( "info-table" ) . rows [ i ] . cells [ 4 ] . innerHTML = "-" ;
123
+ const returnBtn = ( click ) => {
124
+ buildTable . rows [ click ] . cells [ 5 ] . innerHTML = `<button id="borrow" onclick="borrowBtn (${ click } )" >Borrow</button>` ;
125
+ buildTable . rows [ click ] . cells [ 4 ] . innerHTML = "-" ;
127
126
}
128
127
129
- function borrowClick ( i ) {
130
- document . getElementById ( "info-table" ) . rows [ i ] . cells [ 5 ] . innerHTML = `<button id="return" onclick="returnClick (${ i } )">Return</button>` ;
131
- document . getElementById ( "info-table" ) . rows [ i ] . cells [ 4 ] . innerHTML = username ;
132
- }
128
+ const borrowBtn = ( click ) => {
129
+ buildTable . rows [ click ] . cells [ 5 ] . innerHTML = `<button id="return" onclick="returnBtn (${ click } )" >Return</button>` ;
130
+ buildTable . rows [ click ] . cells [ 4 ] . innerHTML = userId ;
131
+ }
0 commit comments