Skip to content

Commit 67aa735

Browse files
committed
all upadted
1 parent 8f9f08a commit 67aa735

File tree

4 files changed

+114
-111
lines changed

4 files changed

+114
-111
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Ankit Raj - 1906534
2+
13
We have a simple BookClub database that is maintaining a list of books, who has lent the book, and who has borrowed the book. We can also add new book entries, borrow and return books.
24

35
The database looks something like this (The actions column will have buttons appearing conditionally based on the state of the application).
@@ -13,8 +15,10 @@ The database looks something like this (The actions column will have buttons app
1315

1416
Here is what the completed page looks for the logged out and logged in user
1517

16-
![Screenshot 2022-03-02 at 10 11 39 PM](https://imgur.com/JfbzFsM.png)
18+
![Screenshot 2022-03-02 at 10 11 28 PM](https://imgur.com/JfbzFsM.png)
19+
1720

21+
![Screenshot 2022-03-02 at 10 11 39 PM](https://imgur.com/yvJPDga.png)
1822

1923

2024
For purposes of this exercise, we have provided the basic HTML and CSS styles for you. You only need to write the Javascript code.

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@
5252
</section>
5353
<!-- JavaScript Bundle with Popper -->
5454
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2" crossorigin="anonymous"></script>
55-
<script type="text/javascript" src="library.js"></script>
55+
<script type="text/javascript" src="./library.js"></script>
5656
</body>
57-
</html>
57+
</html>

library.js

Lines changed: 106 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,131 @@
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 = '-';
118

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>`;
2922
}
3023
}
31-
for (var i = 0; i < 6; i++) {
32-
addDataRow(i);
33-
}
3424

25+
tableData();
3526

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";
3632

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;
5046
}
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 = "-";
5450
}
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;
6058
}
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);
6664
}
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 = "-";
7575
}
7676
}
7777
}
7878
}
7979
}
8080

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>
9795
</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!");
10199
} 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+
}
109109
}
110110
}
111111

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>`;
122121
}
123122

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 = "-";
127126
}
128127

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+
}

style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ table{
3232
border: 1px solid white;
3333
padding: 10px 20px;
3434
text-align: center;
35-
}
35+
}

0 commit comments

Comments
 (0)