From 4a0a2687f5b77a5de9e412d98ed924ff07d1e299 Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Wed, 17 Dec 2025 23:54:02 +0000 Subject: [PATCH 1/3] book laibrary has been completed --- debugging/book-library/index.html | 35 +----- debugging/book-library/script.js | 197 +++++++++++++++++------------- debugging/book-library/style.css | 21 ++++ 3 files changed, 141 insertions(+), 112 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..2ba1cf41 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -30,21 +30,9 @@

Library

- + - + Library value="" />Read - +
@@ -80,15 +63,9 @@

Library

- - - - - - - - - + + + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..858f2671 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,23 +1,23 @@ let myLibrary = []; -window.addEventListener("load", function (e) { - populateStorage(); - render(); +window.addEventListener("load", function () { + populateStorage(); + render(); }); function populateStorage() { - if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); - let book2 = new Book( - "The Old Man and the Sea", - "Ernest Hemingway", - "127", - true - ); - myLibrary.push(book1); - myLibrary.push(book2); - render(); - } + if (myLibrary.length === 0) { + let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); + let book2 = new Book( + "The Old Man and the Sea", + "Ernest Hemingway", + "127", + true + ); + myLibrary.push(book1); + myLibrary.push(book2); + render(); + } } const title = document.getElementById("title"); @@ -25,79 +25,110 @@ const author = document.getElementById("author"); const pages = document.getElementById("pages"); const check = document.getElementById("check"); -//check the right input from forms and if its ok -> add the new book (object in array) -//via Book function and start render function -function submit() { - if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" - ) { - alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); +// Check the input from forms and, if valid, add the new book (object in array) +// via Book constructor and start render function +function addBook() { + if ( + title.value == null || + title.value === "" || + pages.value == null || + pages.value === "" + ) { + showError("Please fill all fields!"); + return false; + } + + if (myLibrary.some(b => b.title === title.value && b.author === author.value)) { + showError("This book already exists!"); + return; + } + + if (!isNaN(title.value.trim()) || !isNaN(author.value.trim())) { + showError("Title and Author must be text!"); + return; + } + + let book = new Book(title.value, author.value, Number(pages.value), check.checked); + myLibrary.push(book); render(); - } } -function Book(title, author, pages, check) { - this.title = title; - this.author = author; - this.pages = pages; - this.check = check; +// Clear any previous error messages +document.getElementById("errorMessage").style.display = "none"; + +function Book(title, author, pages, read) { + this.title = title; + this.author = author; + this.pages = pages; + this.read = read; } + function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; - //delete old table - for (let n = rowsNumber - 1; n > 0; n-- { - table.deleteRow(n); - } - //insert updated row and cells - let length = myLibrary.length; - for (let i = 0; i < length; i++) { - let row = table.insertRow(1); - let titleCell = row.insertCell(0); - let authorCell = row.insertCell(1); - let pagesCell = row.insertCell(2); - let wasReadCell = row.insertCell(3); - let deleteCell = row.insertCell(4); - titleCell.innerHTML = myLibrary[i].title; - authorCell.innerHTML = myLibrary[i].author; - pagesCell.innerHTML = myLibrary[i].pages; - - //add and wait for action for read/unread button - let changeBut = document.createElement("button"); - changeBut.id = i; - changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); - let readStatus = ""; - if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { - readStatus = "No"; + let table = document.getElementById("display"); + let rowsNumber = table.rows.length; + + // Delete old table rows + for (let n = rowsNumber - 1; n > 0; n--) { + table.deleteRow(n); + } + + // Insert updated rows and cells + let length = myLibrary.length; + for (let i = 0; i < length; i++) { + let row = table.insertRow(1); + let titleCell = row.insertCell(0); + let authorCell = row.insertCell(1); + let pagesCell = row.insertCell(2); + let wasReadCell = row.insertCell(3); + let deleteCell = row.insertCell(4); + + titleCell.innerHTML = myLibrary[i].title; + authorCell.innerHTML = myLibrary[i].author; + pagesCell.innerHTML = myLibrary[i].pages; + + // Add and configure read/unread button + let changeBut = document.createElement("button"); + changeBut.id = i; + changeBut.className = "btn btn-primary"; + wasReadCell.appendChild(changeBut); + + let readStatus = myLibrary[i].check ? "Yes" : "No"; + changeBut.innerHTML = readStatus; + + if (myLibrary[i].check) { + changeBut.className = "btn btn-success"; + } + + changeBut.addEventListener("click", function () { + myLibrary[i].check = !myLibrary[i].check; + render(); + }); + + // Add delete button to each row + let delBut = document.createElement("button"); + delBut.id = i + 5; + delBut.className = "btn btn-warning"; + delBut.innerHTML = "Delete"; + deleteCell.appendChild(delBut); + + delBut.setAttribute("data-index", i); +delBut.addEventListener("click", function () { + let index = this.getAttribute("data-index"); + showError(`You've deleted title: ${myLibrary[index].title}`); + myLibrary.splice(index, 1); + render(); +}); } - changeBut.innerText = readStatus; - - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; - render(); - }); - - //add delete button to every row and render again - let delButton = document.createElement("button"); - delBut.id = i + 5; - deleteCell.appendChild(delBut); - delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); - myLibrary.splice(i, 1); - render(); - }); - } } + +// Handle error messages +function showError(message) { + let errorMessCont = document.getElementById("errorMessage"); + errorMessCont.textContent = message; + errorMessCont.classList.add("show"); + + setTimeout(() => { + errorMessCont.classList.remove("show"); + }, 3000); +} \ No newline at end of file diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..ed712f67 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -17,3 +17,24 @@ button.btn-info { margin: 20px; } +#errorMessage { + margin-right: 0; + color: white; + background-color: hsla(0, 88%, 42%, 0.7); + padding: 20px; + border-radius: 5px; + width: 30%; + text-align: center; + font-weight: bold; + display:none ; + position: absolute; + z-index: 1000; + top: 20px; + right: 20px +} + +#errorMessage.show { + display: block; + opacity: 1; + transform: translateX(0) +} From 05f6ca026a39115eb5a8e2d0a039613f2d6922b3 Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Fri, 19 Dec 2025 18:17:38 +0000 Subject: [PATCH 2/3] Add error message and improving the code --- debugging/book-library/index.html | 115 +++++++---------- debugging/book-library/script.js | 208 +++++++++++++----------------- debugging/book-library/style.css | 28 ++-- 3 files changed, 153 insertions(+), 198 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 2ba1cf41..0173d458 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,73 +1,54 @@ - - - - - - - - - + + + Library + + + + + +
+

Library

+

Add books to your virtual library

+
+
+ +
+
+ + + + + + - -
-

Library

-

Add books to your virtual library

-
- - + -
-
- - - - - - - - -
+
+
+ + + + + + + + + + + +
TitleAuthorNumber of PagesRead
+ + + + + - - - - - - - - - - - - - -
TitleAuthorNumber of PagesRead
- - - - + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 858f2671..bb0a45fa 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,134 +1,108 @@ let myLibrary = []; -window.addEventListener("load", function () { - populateStorage(); - render(); +const titleInput = document.getElementById("titleInput"); +const authorInput = document.getElementById("authorInput"); +const pagesInput = document.getElementById("pagesInput"); +const readInput = document.getElementById("readInput"); +const errorBox = document.getElementById("errorMessage"); +const tableBody = document.querySelector("#display tbody"); + +window.addEventListener("load", () => { + populateInitialBooks(); + render(); }); -function populateStorage() { - if (myLibrary.length === 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); - let book2 = new Book( - "The Old Man and the Sea", - "Ernest Hemingway", - "127", - true - ); - myLibrary.push(book1); - myLibrary.push(book2); - render(); - } +function Book(title, author, pages, read) { + this.title = title; + this.author = author; + this.pages = pages; // number + this.read = read; // boolean } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +function populateInitialBooks() { + if (myLibrary.length > 0) return; + + myLibrary.push( + new Book("Robinson Crusoe", "Daniel Defoe", 252, true), + new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true) + ); +} -// Check the input from forms and, if valid, add the new book (object in array) -// via Book constructor and start render function function addBook() { - if ( - title.value == null || - title.value === "" || - pages.value == null || - pages.value === "" - ) { - showError("Please fill all fields!"); - return false; - } - - if (myLibrary.some(b => b.title === title.value && b.author === author.value)) { - showError("This book already exists!"); - return; - } - - if (!isNaN(title.value.trim()) || !isNaN(author.value.trim())) { - showError("Title and Author must be text!"); - return; - } - - let book = new Book(title.value, author.value, Number(pages.value), check.checked); - myLibrary.push(book); - render(); + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + const pages = pagesInput.value.trim(); + const read = readInput.checked; + + if (!title || !author || !pages) { + showError("Please fill all fields!"); + return; + } + + if (isNaN(Number(pages)) || Number(pages) <= 0) { + showError("Pages must be a positive number."); + return; + } + + if (myLibrary.some(b => b.title === title && b.author === author)) { + showError("This book already exists!"); + return; + } + + const book = new Book(title, author, Number(pages), read); + myLibrary.push(book); + clearForm(); + render(); } -// Clear any previous error messages -document.getElementById("errorMessage").style.display = "none"; +document.getElementById("submitBtn").addEventListener("click", addBook); -function Book(title, author, pages, read) { - this.title = title; - this.author = author; - this.pages = pages; - this.read = read; +function clearForm() { + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + readInput.checked = false; } - function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; - - // Delete old table rows - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); - } - - // Insert updated rows and cells - let length = myLibrary.length; - for (let i = 0; i < length; i++) { - let row = table.insertRow(1); - let titleCell = row.insertCell(0); - let authorCell = row.insertCell(1); - let pagesCell = row.insertCell(2); - let wasReadCell = row.insertCell(3); - let deleteCell = row.insertCell(4); - - titleCell.innerHTML = myLibrary[i].title; - authorCell.innerHTML = myLibrary[i].author; - pagesCell.innerHTML = myLibrary[i].pages; - - // Add and configure read/unread button - let changeBut = document.createElement("button"); - changeBut.id = i; - changeBut.className = "btn btn-primary"; - wasReadCell.appendChild(changeBut); - - let readStatus = myLibrary[i].check ? "Yes" : "No"; - changeBut.innerHTML = readStatus; - - if (myLibrary[i].check) { - changeBut.className = "btn btn-success"; - } - - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; - render(); - }); - - // Add delete button to each row - let delBut = document.createElement("button"); - delBut.id = i + 5; - delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - deleteCell.appendChild(delBut); - - delBut.setAttribute("data-index", i); -delBut.addEventListener("click", function () { - let index = this.getAttribute("data-index"); - showError(`You've deleted title: ${myLibrary[index].title}`); - myLibrary.splice(index, 1); - render(); -}); - } + tableBody.innerHTML = ""; + + myLibrary.forEach((book, index) => { + const row = tableBody.insertRow(); + + const titleCell = row.insertCell(); + const authorCell = row.insertCell(); + const pagesCell = row.insertCell(); + const readCell = row.insertCell(); + const deleteCell = row.insertCell(); + + titleCell.textContent = book.title; + authorCell.textContent = book.author; + pagesCell.textContent = book.pages; + + const toggleBtn = document.createElement("button"); + toggleBtn.className = book.read ? "btn btn-success" : "btn btn-primary"; + toggleBtn.textContent = book.read ? "Yes" : "No"; + toggleBtn.addEventListener("click", () => { + book.read = !book.read; + render(); + }); + readCell.appendChild(toggleBtn); + + const delBtn = document.createElement("button"); + delBtn.className = "btn btn-warning"; + delBtn.textContent = "Delete"; + delBtn.addEventListener("click", () => { + myLibrary.splice(index, 1); + render(); + showError(`Deleted: ${book.title}`); + }); + deleteCell.appendChild(delBtn); + }); } -// Handle error messages function showError(message) { - let errorMessCont = document.getElementById("errorMessage"); - errorMessCont.textContent = message; - errorMessCont.classList.add("show"); - - setTimeout(() => { - errorMessCont.classList.remove("show"); - }, 3000); -} \ No newline at end of file + errorBox.textContent = message; + errorBox.classList.add("show"); + setTimeout(() => errorBox.classList.remove("show"), 3000); +} diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index ed712f67..8730a0c0 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,7 +1,6 @@ .form-group { width: 400px; height: 300px; - align-self: left; padding-left: 20px; } @@ -11,30 +10,31 @@ .form-check-label { padding-left: 20px; - margin: 5px 0px 5px 0px; + margin: 5px 0; } button.btn-info { margin: 20px; } + #errorMessage { margin-right: 0; - color: white; - background-color: hsla(0, 88%, 42%, 0.7); - padding: 20px; + color: white; + background-color: hsla(0, 88%, 42%, 0.7); + padding: 20px; border-radius: 5px; - width: 30%; - text-align: center; - font-weight: bold; - display:none ; + width: 30%; + text-align: center; + font-weight: bold; + display: none; position: absolute; - z-index: 1000; - top: 20px; - right: 20px + z-index: 1000; + top: 20px; + right: 20px; } #errorMessage.show { display: block; opacity: 1; - transform: translateX(0) -} + transform: translateX(0); +} \ No newline at end of file From 5c86e22f119387b900b6204e7e50bba40b3f31c2 Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Fri, 19 Dec 2025 22:11:25 +0000 Subject: [PATCH 3/3] Refactor page number validation --- debugging/book-library/index.html | 1 - debugging/book-library/script.js | 8 +++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 0173d458..fefc1151 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -50,5 +50,4 @@

Library

- \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index bb0a45fa..e8d1aa79 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -39,8 +39,9 @@ function addBook() { return; } - if (isNaN(Number(pages)) || Number(pages) <= 0) { - showError("Pages must be a positive number."); + const pageNum = Number(pages); + if (!Number.isInteger(pageNum) || pageNum <= 0) { + showError("Pages must be a positive whole number."); return; } @@ -49,12 +50,13 @@ function addBook() { return; } - const book = new Book(title, author, Number(pages), read); + const book = new Book(title, author, pageNum, read); myLibrary.push(book); clearForm(); render(); } + document.getElementById("submitBtn").addEventListener("click", addBook); function clearForm() {