From f4be6a3b1fb8b43bfd28d967a18bc3f26276b396 Mon Sep 17 00:00:00 2001 From: ahmadehsas Date: Thu, 4 Dec 2025 20:20:52 +0000 Subject: [PATCH 1/7] Fix syntax error. --- debugging/book-library/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..eed706fd 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -54,7 +54,7 @@ function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; //delete old table - for (let n = rowsNumber - 1; n > 0; n-- { + for (let n = rowsNumber - 1; n > 0; n--) { table.deleteRow(n); } //insert updated row and cells From 01c48110b7904da2248a2e6f946dffd75f3ddc52 Mon Sep 17 00:00:00 2001 From: ahmadehsas Date: Thu, 4 Dec 2025 22:30:55 +0000 Subject: [PATCH 2/7] Fix error in console when trying to add book. --- debugging/book-library/index.html | 11 +++++------ debugging/book-library/script.js | 19 +++++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..c5773de7 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -61,12 +61,11 @@

Library

value="" />Read - + + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index eed706fd..81ccb08a 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -27,21 +27,20 @@ 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() { +function addBook() { if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" + !title.value.trim() || + !author.value.trim() || + !pages.value.trim() ) { alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); + return; + } + let book = new Book(title.value.trim(), author.value.trim(), pages.value, check.checked); + myLibrary.push(book); render(); } -} + function Book(title, author, pages, check) { this.title = title; From de116f7219a4c9e3de654f207a5da8865b477070 Mon Sep 17 00:00:00 2001 From: ahmadehsas Date: Thu, 4 Dec 2025 23:47:07 +0000 Subject: [PATCH 3/7] Fix the delete button error. --- debugging/book-library/script.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 81ccb08a..b67b0444 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -75,7 +75,7 @@ function render() { changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == false) { + if (myLibrary[i].check) { readStatus = "Yes"; } else { readStatus = "No"; @@ -89,14 +89,23 @@ function 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(); + delButton.className = "btn btn-primary"; + delButton.textContent = "Delete"; + delButton.addEventListener("click", function() { + if (confirm(`Delete "${myLibrary[i].title}" from your library?`)) { + myLibrary.splice(i, 1); + render(); + } + }); + deleteCell.appendChild(delButton); + // 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(); + } } From 8c4795ecb801c074a3d09807963231e6ab0eb516 Mon Sep 17 00:00:00 2001 From: ahmadehsas Date: Fri, 5 Dec 2025 00:23:04 +0000 Subject: [PATCH 4/7] Fix the error. --- debugging/book-library/script.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index b67b0444..03f891db 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -42,11 +42,11 @@ function addBook() { } -function Book(title, author, pages, check) { +function Book(title, author, pages, read) { this.title = title; this.author = author; this.pages = pages; - this.check = check; + this.check = read; } function render() { From 7e65169ec869f89c4cc482bd3b8f53a38412b044 Mon Sep 17 00:00:00 2001 From: ahmadehsas Date: Sun, 7 Dec 2025 13:59:15 +0000 Subject: [PATCH 5/7] Update index.html and script.js code. --- debugging/book-library/index.html | 102 ++++++++++++++---------------- debugging/book-library/script.js | 98 ++++++++++------------------ 2 files changed, 83 insertions(+), 117 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index c5773de7..031d65f0 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,9 @@ - - + Book Library + + @@ -23,50 +20,55 @@

Library

Add books to your virtual library

-
-
- - - - - - -
+
@@ -80,16 +82,10 @@

Library

- - - - - - - +
- + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 03f891db..496f6d03 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,22 +1,15 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +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("Robinson Crusoe", "Daniel Defoe", "252", true); + let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", "127", true); + myLibrary.push(book1, book2); } } @@ -25,23 +18,18 @@ 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 +// Add a new book to the library function addBook() { - if ( - !title.value.trim() || - !author.value.trim() || - !pages.value.trim() - ) { + if (!title.value.trim() || !author.value.trim() || !pages.value.trim()) { alert("Please fill all fields!"); return; - } - let book = new Book(title.value.trim(), author.value.trim(), pages.value, check.checked); - myLibrary.push(book); - render(); } + let book = new Book(title.value.trim(), author.value.trim(), pages.value, check.checked); + myLibrary.push(book); + render(); +} - +// Book constructor function Book(title, author, pages, read) { this.title = title; this.author = author; @@ -49,63 +37,45 @@ function Book(title, author, pages, read) { this.check = read; } +// Render the library table function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; - //delete old table + + // Clear existing rows (except the header) 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++) { + + // Populate the table with updated library data + myLibrary.forEach((book, 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; + row.insertCell(0).innerText = book.title; + row.insertCell(1).innerText = book.author; + row.insertCell(2).innerText = book.pages; - //add and wait for action for read/unread button + // Add read/unread toggle button + let wasReadCell = row.insertCell(3); let changeBut = document.createElement("button"); - changeBut.id = i; changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); - let readStatus = ""; - if (myLibrary[i].check) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; - + changeBut.innerText = book.check ? "Yes" : "No"; changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + book.check = !book.check; render(); }); + wasReadCell.appendChild(changeBut); - //add delete button to every row and render again + // Add delete button + let deleteCell = row.insertCell(4); let delButton = document.createElement("button"); delButton.className = "btn btn-primary"; delButton.textContent = "Delete"; - delButton.addEventListener("click", function() { - if (confirm(`Delete "${myLibrary[i].title}" from your library?`)) { - myLibrary.splice(i, 1); - render(); + delButton.addEventListener("click", function () { + if (confirm(`Delete "${book.title}" from your library?`)) { + myLibrary.splice(i, 1); + render(); } - }); deleteCell.appendChild(delButton); - // 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(); - - } -} + }); +} \ No newline at end of file From 6e184cf02d4b3058f451d91aa8e788d5dc665886 Mon Sep 17 00:00:00 2001 From: ahmadehsas Date: Mon, 8 Dec 2025 12:19:54 +0000 Subject: [PATCH 6/7] Update addBook function with improved validation and optimized input handling. --- debugging/book-library/script.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 496f6d03..1a33512c 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -20,15 +20,29 @@ const check = document.getElementById("check"); // Add a new book to the library function addBook() { - if (!title.value.trim() || !author.value.trim() || !pages.value.trim()) { + const titleValue = title.value.trim(); + const authorValue = author.value.trim(); + const pagesValue = pages.value.trim(); + const isRead = check.checked; + + // Validate input fields + if (!titleValue || !authorValue || !pagesValue) { alert("Please fill all fields!"); - return; + return false; + } + + // Validate "number of pages" field + const pagesNumber = parseInt(pagesValue, 10); + if (isNaN(pagesNumber) || pagesNumber <= 0) { + alert("Please enter a valid number of pages!"); + return false; } - let book = new Book(title.value.trim(), author.value.trim(), pages.value, check.checked); + + // Create a new book and add it to the library + const book = new Book(titleValue, authorValue, pagesNumber, isRead); myLibrary.push(book); render(); } - // Book constructor function Book(title, author, pages, read) { this.title = title; From d6df415b7a353dd2db011615e777f5908f5f930c Mon Sep 17 00:00:00 2001 From: ahmadehsas Date: Mon, 8 Dec 2025 12:28:10 +0000 Subject: [PATCH 7/7] Update the function populateStorage ro store the page counts as numbers instead of strings. --- debugging/book-library/script.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 1a33512c..b6c5ba63 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -7,8 +7,8 @@ window.addEventListener("load", function () { function populateStorage() { if (myLibrary.length === 0) { - let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true); - let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", "127", true); + let book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true); // Changed "252" to 252 + let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true); // Changed "127" to 127 myLibrary.push(book1, book2); } }