From c830178f88d8b7aedb8cc1c96f4cf82135890d9f Mon Sep 17 00:00:00 2001 From: Aida Zhukova Date: Fri, 28 Mar 2025 16:15:07 +0000 Subject: [PATCH 01/17] bugs are fixed now --- debugging/book-library/script.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..09cb565b 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -31,14 +31,16 @@ function submit() { if ( title.value == null || title.value == "" || - pages.value == null || - pages.value == "" + pages.value == null || + pages.value == "" || + author.value == null || + author.value == "" ) { alert("Please fill all fields!"); return false; } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); + let book = new Book(title.value, author.value, pages.value, check.checked); + myLibrary.push(book); render(); } } @@ -53,8 +55,10 @@ function Book(title, author, pages, check) { function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; + console.log(myLibrary) + //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 @@ -76,7 +80,7 @@ function render() { changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == false) { + if (myLibrary[i].check == true) { readStatus = "Yes"; } else { readStatus = "No"; @@ -90,11 +94,11 @@ 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 () { + delButton.id = i + 5; + deleteCell.appendChild(delButton); + delButton.className = "btn btn-warning"; + delButton.innerHTML = "Delete"; + delButton.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From c3b7f29751c33a6788fd1db5d09d14099efc879f Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Sat, 9 Aug 2025 03:31:42 +0100 Subject: [PATCH 02/17] fixed the if condition's syntax of the missing bracket. --- 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 e99d6aa32650b65270c1cf8b97e3bbf3e45c0ea8 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Sat, 9 Aug 2025 03:34:56 +0100 Subject: [PATCH 03/17] added a title to the web page and a languagge element to the html opener. --- debugging/book-library/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..44a0e47e 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,7 +1,7 @@ - + - + Book library Date: Sat, 16 Aug 2025 14:04:22 +0100 Subject: [PATCH 04/17] updated html code --- debugging/book-library/index.html | 65 +++++++++---------------------- 1 file changed, 18 insertions(+), 47 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 44a0e47e..f092274b 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -2,19 +2,12 @@ Book library - + - - + + @@ -28,45 +21,23 @@

Library

-
+
- + + - + + - - - + + +
+ + +
+ + +
@@ -77,7 +48,7 @@

Library

Author Number of Pages Read - + Action From 300dc3915885c619682e9c2298c95c30d582d3cb Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Sat, 16 Aug 2025 14:28:26 +0100 Subject: [PATCH 05/17] updated css. --- debugging/book-library/style.css | 119 +++++++++++++++++++++++++++---- 1 file changed, 107 insertions(+), 12 deletions(-) diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..a82f0ba8 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,19 +1,114 @@ -.form-group { - width: 400px; - height: 300px; - align-self: left; - padding-left: 20px; +/* General page styling */ +body { + background: #f7f9fc; + font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; + color: #333; + margin: 0; + padding: 0; } -.btn { - display: block; +/* Jumbotron header */ +.jumbotron { + background: linear-gradient(135deg, #4facfe, #00f2fe); + color: white; + padding: 3rem 2rem; + border-radius: 0 0 20px 20px; + box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15); } -.form-check-label { - padding-left: 20px; - margin: 5px 0px 5px 0px; +/* Add Book button */ +button.btn-info { + margin: 1rem 0; + background: #03cceb; + border: none; + width: 15%; + height: 30px; + transition: background 0.3s ease, transform 0.2s ease; } -button.btn-info { - margin: 20px; +button.btn-info:hover { + background: #138496; + transform: translateY(-2px); +} + +/* Form styling */ +#demo { + background: #fff; + padding: 1.5rem; + margin: 1rem 0; + border-radius: 12px; + box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); +} + +#bookForm label { + font-weight: 600; + margin-top: 0.5rem; +} + +#bookForm input[type="text"], +#bookForm input[type="number"] { + border-radius: 8px; + border: 1px solid #ccc; + padding: 0.5rem; + margin-bottom: 0.8rem; +} + +#bookForm input:focus { + border-color: #17a2b8; + box-shadow: 0 0 5px rgba(23, 162, 184, 0.4); +} + +#submitBtn { + width: 10%; + border-radius: 8px; + padding: 0.6rem; + font-weight: 600; +} + +/* Table styling */ +.table { + background: #fff; + border-radius: 12px; + overflow: hidden; + margin-top: 2rem; + box-shadow: 0 3px 12px rgba(0, 0, 0, 0.08); +} + +.table th { + background: #343a40 !important; + color: #fff; + text-align: center; + vertical-align: middle; +} + +.table td { + text-align: center; + vertical-align: middle; +} + +/* Delete button */ +.delete-btn { + background: #e74c3c; + border: none; + color: white; + padding: 6px 12px; + border-radius: 6px; + cursor: pointer; + font-size: 0.85rem; + transition: background 0.3s ease; +} + +.delete-btn:hover { + background: #c0392b; +} + +/* Checkbox styling */ +.form-check-input { + transform: scale(1.2); + margin-right: 8px; +} + +/* Smooth transitions */ +* { + transition: all 0.2s ease-in-out; } From 6b2c9a991a167a87b0db6bee07ca62747a0fbcf4 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Sat, 16 Aug 2025 15:45:42 +0100 Subject: [PATCH 06/17] updated the script file. --- debugging/book-library/script.js | 92 ++++++++++++-------------------- 1 file changed, 34 insertions(+), 58 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 09cb565b..4e25069c 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,8 +1,7 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", function () { populateStorage(); - render(); }); function populateStorage() { @@ -23,85 +22,62 @@ function populateStorage() { const title = document.getElementById("title"); const author = document.getElementById("author"); const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +const read = document.getElementById("read"); + +document.getElementById("submitBtn").addEventListener("click", addBook); //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 == "" || - author.value == null || - author.value == "" - ) { +function addBook() { + if (!title.value || !author.value || !pages.value) { alert("Please fill all fields!"); - return false; - } else { + return; + } let book = new Book(title.value, author.value, pages.value, check.checked); myLibrary.push(book); render(); + document.getElementById("bookForm").reset(); + } -} -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.read = read; } - function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; - console.log(myLibrary) + let tbody = document.querySelector("#display tbody"); + tbody.innerHTML = ""; - //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; + myLibrary.forEach((book, i) => { + let row = tbody.insertRow(); + + row.insertCell(0).textContent = book.title; + row.insertCell(1).textContent = book.author; + row.insertCell(2).textContent = book.pages; - //add and wait for action for read/unread button + // read/unread 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 == true) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; - - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + changeBut.textContent = book.read ? "Yes" : "No"; + changeBut.addEventListener("click", () => { + book.read = !book.read; render(); }); + wasReadCell.appendChild(changeBut); - //add delete button to every row and render again + // delete button + let deleteCell = row.insertCell(4); let delButton = document.createElement("button"); - delButton.id = i + 5; - deleteCell.appendChild(delButton); - delButton.className = "btn btn-warning"; - delButton.innerHTML = "Delete"; - delButton.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); + delButton.className = "delete-btn"; + delButton.textContent = "Delete"; + delButton.addEventListener("click", () => { + alert(`You've deleted title: ${book.title}`); myLibrary.splice(i, 1); render(); }); - } + deleteCell.appendChild(delButton); + }); } From c21b78775f9250d44fd8743da31f7b624bd92221 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Sun, 17 Aug 2025 01:52:43 +0100 Subject: [PATCH 07/17] updated and validated html file. --- debugging/book-library/index.html | 18 +++++++++--------- debugging/book-library/script.js | 8 ++------ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index f092274b..89ea4925 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,13 @@ - Book library - + Book library + + - + @@ -16,7 +17,7 @@

Library

Add books to your virtual library

- @@ -25,20 +26,19 @@

Library

- +
- +
- + - @@ -64,4 +64,4 @@

Library

- + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 4e25069c..1c4b7166 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -2,17 +2,13 @@ let myLibrary = []; 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 - ); + let book2 = new Book("The Old Man and the Sea","Ernest Hemingway","127", true ); myLibrary.push(book1); myLibrary.push(book2); render(); From 5fd20349555fba67b8871c55d4196ccd36ca539e Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Sun, 17 Aug 2025 02:24:09 +0100 Subject: [PATCH 08/17] updated the input values not to be empty spaces. --- debugging/book-library/script.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 1c4b7166..0228d3c8 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -18,22 +18,21 @@ function populateStorage() { const title = document.getElementById("title"); const author = document.getElementById("author"); const pages = document.getElementById("pages"); -const read = document.getElementById("read"); +const check = document.getElementById("check"); document.getElementById("submitBtn").addEventListener("click", addBook); //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 addBook() { - if (!title.value || !author.value || !pages.value) { - alert("Please fill all fields!"); + if (!title.value.trim() || !author.value.trim() || !pages.value.trim()) { + alert("Please fill all fields with valide values (no empty spaces)!"); return; } - let book = new Book(title.value, author.value, pages.value, check.checked); + let book = new Book(title.value.trim(), author.value.trim(), pages.value.trim(), check.checked); myLibrary.push(book); render(); document.getElementById("bookForm").reset(); - } function Book(title, author, pages, read) { From 808cd280f4171232a51e4467b640afdb9c9dca90 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Sun, 17 Aug 2025 02:30:16 +0100 Subject: [PATCH 09/17] updated css file. --- debugging/book-library/style.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index a82f0ba8..131f6607 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -27,7 +27,7 @@ button.btn-info { } button.btn-info:hover { - background: #138496; + background: #4facfe; transform: translateY(-2px); } From 6473c1fc715fa4d1e216ad4d76947bb3fef8c69a Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Tue, 19 Aug 2025 00:28:11 +0100 Subject: [PATCH 10/17] Cache DOM elements with clear suffixes --- debugging/book-library/script.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 0228d3c8..0128f91b 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,9 +1,16 @@ let myLibrary = []; -window.addEventListener("load", function () { - populateStorage(); - render(); -}); +window.onload = function(){ + // Cache DOM elements with clear suffixes + const titleInputEl = document.getElementById("title"); + const authorInputEl = document.getElementById("author"); + const pagesInputEl = document.getElementById("pages"); + const readCheckEl = document.getElementById("check"); + const submitBtnEl = document.getElementById("submitBtn"); + const bookFormEl = document.getElementById("bookForm"); + const tbodyEl = document.querySelector("#display tbody"); + +} function populateStorage() { if (myLibrary.length == 0) { From 60240cba645e4f183954a274cc68a02a394b2d7d Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Tue, 19 Aug 2025 00:34:33 +0100 Subject: [PATCH 11/17] Grouped all setup codes in window.onload --- debugging/book-library/script.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 0128f91b..19646703 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -10,25 +10,22 @@ window.onload = function(){ const bookFormEl = document.getElementById("bookForm"); const tbodyEl = document.querySelector("#display tbody"); -} + // Initial setup + populateStorage(); + render(); + + // Event listeners + submitBtnEl.addEventListener("click", addBook); + // --- Functions --- 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); + myLibrary.push(book1, book2); render(); } } - -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); - -document.getElementById("submitBtn").addEventListener("click", addBook); - //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 addBook() { From 196a070c0af592a014283a283748558a83e8e9eb Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Tue, 19 Aug 2025 00:46:33 +0100 Subject: [PATCH 12/17] improved function to addbook --- debugging/book-library/script.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 19646703..3df5d8bb 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -25,18 +25,26 @@ function populateStorage() { myLibrary.push(book1, book2); render(); } + } } -//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 addBook() { - if (!title.value.trim() || !author.value.trim() || !pages.value.trim()) { - alert("Please fill all fields with valide values (no empty spaces)!"); - return; - } - let book = new Book(title.value.trim(), author.value.trim(), pages.value.trim(), check.checked); + + function addBook() { + // Preprocess & validate input + const title = titleInputEl.value.trim(); + const author = authorInputEl.value.trim(); + const pages = pagesInputEl.value.trim(); + const read = readCheckEl.checked; + + if (!title || !author || !pages || isNaN(Number(pages)) || Number(pages) <= 0) { + alert("Please fill all fields with valid values (no empty spaces, pages must be a positive number)."); + return; + } + + // Add book + let book = new Book(title, author, pages, read); myLibrary.push(book); render(); - document.getElementById("bookForm").reset(); + bookFormEl.reset(); } function Book(title, author, pages, read) { From 2e0943e4e207318dfe12589bc5eb56d171fac45c Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Tue, 19 Aug 2025 00:57:24 +0100 Subject: [PATCH 13/17] Fix delete alert order (show confirmation only after deletion succeeds, not before) --- debugging/book-library/script.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 3df5d8bb..e0f71731 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -54,7 +54,6 @@ function Book(title, author, pages, read) { this.read = read; } function render() { - let tbody = document.querySelector("#display tbody"); tbody.innerHTML = ""; myLibrary.forEach((book, i) => { @@ -81,10 +80,10 @@ function render() { delButton.className = "delete-btn"; delButton.textContent = "Delete"; delButton.addEventListener("click", () => { - alert(`You've deleted title: ${book.title}`); - myLibrary.splice(i, 1); - render(); + myLibrary.splice(i, 1); + render(); + alert(`The book "${book.title}" has been deleted successfully.`); }); - deleteCell.appendChild(delButton); + deleteCell.appendChild(delBtnEl); }); } From 555c393145d0fe361970a7f75aa6555188633f41 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Tue, 19 Aug 2025 01:33:09 +0100 Subject: [PATCH 14/17] updated html code. --- debugging/book-library/script.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index e0f71731..737db829 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -26,7 +26,6 @@ function populateStorage() { render(); } } -} function addBook() { // Preprocess & validate input @@ -87,3 +86,5 @@ function render() { deleteCell.appendChild(delBtnEl); }); } + // --- End of functions --- +} \ No newline at end of file From 410104fd59a7b545565676ae03be28ff60635c3a Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Tue, 19 Aug 2025 01:42:16 +0100 Subject: [PATCH 15/17] fixes js code --- debugging/book-library/script.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 737db829..463996ba 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -63,28 +63,27 @@ function render() { row.insertCell(2).textContent = book.pages; // read/unread button - let wasReadCell = row.insertCell(3); - let changeBut = document.createElement("button"); - changeBut.className = "btn btn-success"; - changeBut.textContent = book.read ? "Yes" : "No"; - changeBut.addEventListener("click", () => { + let wasReadCell = row.insertCell(3); + let changeBtnEl = document.createElement("button"); + changeBtnEl.className = "btn btn-success"; + changeBtnEl.textContent = book.read ? "Yes" : "No"; + changeBtnEl.addEventListener("click", () => { book.read = !book.read; render(); }); - wasReadCell.appendChild(changeBut); + wasReadCell.appendChild(changeBtnEl); - // delete button - let deleteCell = row.insertCell(4); - let delButton = document.createElement("button"); - delButton.className = "delete-btn"; - delButton.textContent = "Delete"; - delButton.addEventListener("click", () => { + // delete button + let deleteCell = row.insertCell(4); + let delBtnEl = document.createElement("button"); + delBtnEl.className = "delete-btn"; + delBtnEl.textContent = "Delete"; + delBtnEl.addEventListener("click", () => { myLibrary.splice(i, 1); render(); alert(`The book "${book.title}" has been deleted successfully.`); }); deleteCell.appendChild(delBtnEl); - }); -} - // --- End of functions --- -} \ No newline at end of file + }); + } +}; \ No newline at end of file From b77a783f72395cd95905cf3c5110229523709b14 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Tue, 19 Aug 2025 01:53:14 +0100 Subject: [PATCH 16/17] Improve maintainability / readability --- debugging/book-library/script.js | 69 ++++++++++++++++---------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 463996ba..97f56870 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,7 +1,7 @@ let myLibrary = []; -window.onload = function(){ - // Cache DOM elements with clear suffixes +window.onload = function () { + // Cache DOM elements with clear suffixes const titleInputEl = document.getElementById("title"); const authorInputEl = document.getElementById("author"); const pagesInputEl = document.getElementById("pages"); @@ -10,7 +10,7 @@ window.onload = function(){ const bookFormEl = document.getElementById("bookForm"); const tbodyEl = document.querySelector("#display tbody"); - // Initial setup + // Initial setup populateStorage(); render(); @@ -18,16 +18,16 @@ window.onload = function(){ submitBtnEl.addEventListener("click", addBook); // --- Functions --- -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, book2); - 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, book2); + render(); + } } - } - function addBook() { + function addBook() { // Preprocess & validate input const title = titleInputEl.value.trim(); const author = authorInputEl.value.trim(); @@ -46,32 +46,33 @@ function populateStorage() { bookFormEl.reset(); } -function Book(title, author, pages, read) { - this.title = title; - this.author = author; - this.pages = pages; - this.read = read; -} -function render() { - tbody.innerHTML = ""; + function Book(title, author, pages, read) { + this.title = title; + this.author = author; + this.pages = pages; + this.read = read; + } + + function render() { + tbodyEl.innerHTML = ""; - myLibrary.forEach((book, i) => { - let row = tbody.insertRow(); + myLibrary.forEach((book, i) => { + let row = tbodyEl.insertRow(); - row.insertCell(0).textContent = book.title; - row.insertCell(1).textContent = book.author; - row.insertCell(2).textContent = book.pages; + row.insertCell(0).textContent = book.title; + row.insertCell(1).textContent = book.author; + row.insertCell(2).textContent = book.pages; - // read/unread button - let wasReadCell = row.insertCell(3); + // read/unread button + let wasReadCell = row.insertCell(3); let changeBtnEl = document.createElement("button"); changeBtnEl.className = "btn btn-success"; changeBtnEl.textContent = book.read ? "Yes" : "No"; changeBtnEl.addEventListener("click", () => { - book.read = !book.read; - render(); - }); - wasReadCell.appendChild(changeBtnEl); + book.read = !book.read; + render(); + }); + wasReadCell.appendChild(changeBtnEl); // delete button let deleteCell = row.insertCell(4); @@ -82,8 +83,8 @@ function render() { myLibrary.splice(i, 1); render(); alert(`The book "${book.title}" has been deleted successfully.`); + }); + deleteCell.appendChild(delBtnEl); }); - deleteCell.appendChild(delBtnEl); - }); - } -}; \ No newline at end of file + } +}; From f032d63a26a77fc68aabafb482723c3975986be1 Mon Sep 17 00:00:00 2001 From: Waleed Yahya Date: Tue, 19 Aug 2025 02:10:07 +0100 Subject: [PATCH 17/17] Edited input validation to prevent decimal values (e.g., 12.5 pages). prevent extremely large numbers of pages (e.g., 9999999). checks if author/title length is within reasonable range. --- debugging/book-library/script.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 97f56870..80282a69 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -38,6 +38,19 @@ window.onload = function () { alert("Please fill all fields with valid values (no empty spaces, pages must be a positive number)."); return; } + if (!title || title.length < 2) { + alert("Title must be at least 2 characters long."); + return; +} +if (!author || author.length < 2) { + alert("Author must be at least 2 characters long."); + return; +} +const pageNum = Number(pages); +if (!Number.isInteger(pageNum) || pageNum <= 0 || pageNum > 10000) { + alert("Pages must be a positive whole number (1–10,000)."); + return; +} // Add book let book = new Book(title, author, pages, read);