From 97c6d21382504027e2dfa5899f261a801c29f54b Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Thu, 20 Aug 2026 10:30:32 +0100 Subject: [PATCH 01/10] book lilbrary fix bug --- debugging/book-library/script.js | 121 +++++++++++++++++-------------- 1 file changed, 67 insertions(+), 54 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d3..5c4acccf5 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,22 +1,18 @@ 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 + // Initialize sample data with two books + myLibrary.push(new Book("Robinson Crusoe", "Daniel Defoe", "252", true)); + + myLibrary.push( + new Book("The Old Man and the Sea", "Ernest Hemingway", "127", true) ); - myLibrary.push(book1); - myLibrary.push(book2); - render(); } } @@ -25,22 +21,42 @@ 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 == "" - ) { + // remove empty spaces before and after author and title input + title.value = title.value.trim(); + author.value = author.value.trim(); + + // Check for empty input fields + if (!title.value || !author.value || !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); - render(); + return; + } + + // check if page number is a valid integer > 0 + if (isNaN(pages) || pages <= 0 || !Number.isInteger(pages)) { + alert("number of pages need to be a valid integer >0!"); + return; } + + // Create a new book object with input data + const newBook = new Book( + title.value, + author.value, + pages.value, + check.checked + ); + + // Add the new book to the library + myLibrary.push(newBook); + + // Update the display + render(); + + // Clear the form + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; } function Book(title, author, pages, check) { @@ -53,51 +69,48 @@ function Book(title, author, pages, check) { function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; - //delete old table - for (let n = rowsNumber - 1; n > 0; n-- { + + // Remove existing rows except 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++) { - 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 + + for (let i = 0; i < myLibrary.length; i++) { + let row = table.insertRow(); + + // Insert cells + row.insertCell().innerText = myLibrary[i].title; + row.insertCell().innerText = myLibrary[i].author; + row.insertCell().innerText = myLibrary[i].pages; + + // Read/Unread button + let readCell = row.insertCell(); + 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"; - } - changeBut.innerText = readStatus; + changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; 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); + readCell.appendChild(changeBut); + + // Delete button + let deleteCell = row.insertCell(); + + let delBut = document.createElement("button"); delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delBut.innerText = "Delete"; + + delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); + myLibrary.splice(i, 1); render(); }); + + deleteCell.appendChild(delBut); } } From 16fa2645ef267ec85fb41b989e25a23701bfa4a2 Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Thu, 20 Aug 2026 11:23:56 +0100 Subject: [PATCH 02/10] book library debugging --- debugging/book-library/index.html | 62 ++++++++++++++++++------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71e..e3324922b 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,19 +1,23 @@ - + - + + + Book Library + + + @@ -23,7 +27,12 @@

Library

Add books to your virtual library

- @@ -31,38 +40,46 @@

Library

- + + + - + +
+ +
+ Library + - - - - - - - + - + \ No newline at end of file From 30137019426bbe65e062c09479fa33818333ebfc Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Thu, 20 Aug 2026 11:31:46 +0100 Subject: [PATCH 03/10] fix changes --- debugging/book-library/index.html | 62 +++++++++++++------------------ 1 file changed, 25 insertions(+), 37 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index e3324922b..23acfa71e 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,23 +1,19 @@ - + - + - - Book Library - - - @@ -27,12 +23,7 @@

Library

Add books to your virtual library

- @@ -40,46 +31,38 @@

Library

- - + - - -
- -
- + Library - - + + + + + + + - \ No newline at end of file + From 597ab64cb41e0c89ce59410644cde8d6253b649c Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Thu, 20 Aug 2026 15:08:51 +0100 Subject: [PATCH 04/10] adding file --- debugging/book-library/script.js | 121 ++++++++++++++----------------- 1 file changed, 54 insertions(+), 67 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 5c4acccf5..75ce6c1d3 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,18 +1,22 @@ let myLibrary = []; -window.addEventListener("load", function () { +window.addEventListener("load", function (e) { populateStorage(); render(); }); function populateStorage() { if (myLibrary.length == 0) { - // Initialize sample data with two books - myLibrary.push(new Book("Robinson Crusoe", "Daniel Defoe", "252", true)); - - myLibrary.push( - new Book("The Old Man and the Sea", "Ernest Hemingway", "127", true) + 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(); } } @@ -21,42 +25,22 @@ 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() { - // remove empty spaces before and after author and title input - title.value = title.value.trim(); - author.value = author.value.trim(); - - // Check for empty input fields - if (!title.value || !author.value || !pages.value) { + if ( + title.value == null || + title.value == "" || + pages.value == null || + pages.value == "" + ) { alert("Please fill all fields!"); - return; - } - - // check if page number is a valid integer > 0 - if (isNaN(pages) || pages <= 0 || !Number.isInteger(pages)) { - alert("number of pages need to be a valid integer >0!"); - return; + return false; + } else { + let book = new Book(title.value, title.value, pages.value, check.checked); + library.push(book); + render(); } - - // Create a new book object with input data - const newBook = new Book( - title.value, - author.value, - pages.value, - check.checked - ); - - // Add the new book to the library - myLibrary.push(newBook); - - // Update the display - render(); - - // Clear the form - title.value = ""; - author.value = ""; - pages.value = ""; - check.checked = false; } function Book(title, author, pages, check) { @@ -69,48 +53,51 @@ function Book(title, author, pages, check) { function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; - - // Remove existing rows except header - for (let n = rowsNumber - 1; n > 0; n--) { + //delete old table + for (let n = rowsNumber - 1; n > 0; n-- { table.deleteRow(n); } - - for (let i = 0; i < myLibrary.length; i++) { - let row = table.insertRow(); - - // Insert cells - row.insertCell().innerText = myLibrary[i].title; - row.insertCell().innerText = myLibrary[i].author; - row.insertCell().innerText = myLibrary[i].pages; - - // Read/Unread button - let readCell = row.insertCell(); - + //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"; - changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; + wasReadCell.appendChild(changeBut); + let readStatus = ""; + if (myLibrary[i].check == false) { + readStatus = "Yes"; + } else { + readStatus = "No"; + } + changeBut.innerText = readStatus; changeBut.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); - readCell.appendChild(changeBut); - - // Delete button - let deleteCell = row.insertCell(); - - let delBut = document.createElement("button"); + //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.innerText = "Delete"; - - delBut.addEventListener("click", function () { + delBut.innerHTML = "Delete"; + delBut.addEventListener("clicks", function () { alert(`You've deleted title: ${myLibrary[i].title}`); - myLibrary.splice(i, 1); render(); }); - - deleteCell.appendChild(delBut); } } From 291e3468e040e605af8746a78ab664f401b9036c Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Thu, 20 Aug 2026 18:11:03 +0100 Subject: [PATCH 05/10] adding relevant files --- debugging/book-library/script.js | 121 +++++++++++++++++-------------- 1 file changed, 67 insertions(+), 54 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d3..5c4acccf5 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,22 +1,18 @@ 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 + // Initialize sample data with two books + myLibrary.push(new Book("Robinson Crusoe", "Daniel Defoe", "252", true)); + + myLibrary.push( + new Book("The Old Man and the Sea", "Ernest Hemingway", "127", true) ); - myLibrary.push(book1); - myLibrary.push(book2); - render(); } } @@ -25,22 +21,42 @@ 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 == "" - ) { + // remove empty spaces before and after author and title input + title.value = title.value.trim(); + author.value = author.value.trim(); + + // Check for empty input fields + if (!title.value || !author.value || !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); - render(); + return; + } + + // check if page number is a valid integer > 0 + if (isNaN(pages) || pages <= 0 || !Number.isInteger(pages)) { + alert("number of pages need to be a valid integer >0!"); + return; } + + // Create a new book object with input data + const newBook = new Book( + title.value, + author.value, + pages.value, + check.checked + ); + + // Add the new book to the library + myLibrary.push(newBook); + + // Update the display + render(); + + // Clear the form + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; } function Book(title, author, pages, check) { @@ -53,51 +69,48 @@ function Book(title, author, pages, check) { function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; - //delete old table - for (let n = rowsNumber - 1; n > 0; n-- { + + // Remove existing rows except 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++) { - 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 + + for (let i = 0; i < myLibrary.length; i++) { + let row = table.insertRow(); + + // Insert cells + row.insertCell().innerText = myLibrary[i].title; + row.insertCell().innerText = myLibrary[i].author; + row.insertCell().innerText = myLibrary[i].pages; + + // Read/Unread button + let readCell = row.insertCell(); + 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"; - } - changeBut.innerText = readStatus; + changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; 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); + readCell.appendChild(changeBut); + + // Delete button + let deleteCell = row.insertCell(); + + let delBut = document.createElement("button"); delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delBut.innerText = "Delete"; + + delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); + myLibrary.splice(i, 1); render(); }); + + deleteCell.appendChild(delBut); } } From 8bf9a93f701c05266fad4830436f5a2c48e84c95 Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Thu, 20 Aug 2026 18:30:08 +0100 Subject: [PATCH 06/10] adding files --- 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 302950cb8..9c0ecd9c4 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -15,5 +15,5 @@ } button.btn-info { - margin: 20px; + margin: 30px; } From 269f218b1d42dc7171ba111e70e8f2bc1f09c154 Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Thu, 20 Aug 2026 18:30:40 +0100 Subject: [PATCH 07/10] adding title to html --- debugging/book-library/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71e..4c3a9475a 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,7 +1,7 @@ - + The Book library Date: Fri, 21 Aug 2026 12:23:12 +0100 Subject: [PATCH 08/10] fixing validation issue --- debugging/book-library/index.html | 38 ++++++++++++++++++++++++------- debugging/book-library/script.js | 6 +++-- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 4c3a9475a..6f1b46c7a 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,19 +1,23 @@ - The Book library + The Book Library + + + + @@ -23,50 +27,67 @@

Library

Add books to your virtual library

-
+ + - + + + + + + + + + +
@@ -80,6 +101,7 @@

Library

+ @@ -93,4 +115,4 @@

Library

- + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 5c4acccf5..01587e8e4 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -33,8 +33,10 @@ function submit() { } // check if page number is a valid integer > 0 - if (isNaN(pages) || pages <= 0 || !Number.isInteger(pages)) { - alert("number of pages need to be a valid integer >0!"); + const pageNumber = Number(pages.value); + + if (!Number.isInteger(pageNumber) || pageNumber <= 0) { + alert("Number of pages needs to be a valid integer greater than 0!"); return; } From 10d0aba2f3c17ad204ec8cb203fa0bdce628352e Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Fri, 21 Aug 2026 15:25:42 +0100 Subject: [PATCH 09/10] response to feedback --- debugging/book-library/index.html | 109 ++++++++++++++---------------- 1 file changed, 50 insertions(+), 59 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 6f1b46c7a..003044e21 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,4 +1,4 @@ - + The Book Library @@ -31,64 +31,63 @@

Library

data-toggle="collapse" data-target="#demo" class="btn btn-info" + type="button" > Add new book
-
- - - - - - - - - - - - +
+
+ + + - - - - + + + -
+ +
+ +
+ + + +
+
@@ -102,17 +101,9 @@

Library

- - - - - - - - - +
- \ No newline at end of file + From 030e4f91840cbdc4df9ab4cfde7c716ae6503eda Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Fri, 21 Aug 2026 17:28:45 +0100 Subject: [PATCH 10/10] changes --- debugging/book-library/script.js | 93 +++++++++++++++++++------------- 1 file changed, 57 insertions(+), 36 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 01587e8e4..ff871549f 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,64 +1,79 @@ -let myLibrary = []; +const myLibrary = []; + +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const readInput = document.getElementById("check"); +const form = document.getElementById("form"); window.addEventListener("load", function () { populateStorage(); render(); + + if (form) { + form.addEventListener("submit", function (event) { + event.preventDefault(); + submit(); + }); + } }); function populateStorage() { - if (myLibrary.length == 0) { + if (myLibrary.length === 0) { // Initialize sample data with two books - myLibrary.push(new Book("Robinson Crusoe", "Daniel Defoe", "252", true)); + myLibrary.push(new Book("Robinson Crusoe", "Daniel Defoe", 252, true)); myLibrary.push( - new Book("The Old Man and the Sea", "Ernest Hemingway", "127", true) + new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true) ); } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); - function submit() { - // remove empty spaces before and after author and title input - title.value = title.value.trim(); - author.value = author.value.trim(); + // Get cleaned input values + const titleValue = titleInput.value.trim(); + const authorValue = authorInput.value.trim(); + const pagesValue = pagesInput.value.trim(); // Check for empty input fields - if (!title.value || !author.value || !pages.value) { + if (!titleValue || !authorValue || !pagesValue) { alert("Please fill all fields!"); return; } - // check if page number is a valid integer > 0 - const pageNumber = Number(pages.value); + // Make sure pages contains digits only + if (!/^\d+$/.test(pagesValue)) { + alert("Number of pages needs to be a valid integer greater than 0!"); + return; + } + + // Convert pages to a number + const pageNumber = Number(pagesValue); - if (!Number.isInteger(pageNumber) || pageNumber <= 0) { + // Check that pages is greater than zero + if (pageNumber <= 0) { alert("Number of pages needs to be a valid integer greater than 0!"); return; } - // Create a new book object with input data + // Create a new book using cleaned and validated values const newBook = new Book( - title.value, - author.value, - pages.value, - check.checked + titleValue, + authorValue, + pageNumber, + readInput.checked ); - // Add the new book to the library + // Add the new book myLibrary.push(newBook); // Update the display render(); - // Clear the form - title.value = ""; - author.value = ""; - pages.value = ""; - check.checked = false; + // Reset the entire form + if (form) { + form.reset(); + } } function Book(title, author, pages, check) { @@ -69,8 +84,8 @@ function Book(title, author, pages, check) { } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; + const table = document.getElementById("display"); + const rowsNumber = table.rows.length; // Remove existing rows except header for (let n = rowsNumber - 1; n > 0; n--) { @@ -78,17 +93,17 @@ function render() { } for (let i = 0; i < myLibrary.length; i++) { - let row = table.insertRow(); + const row = table.insertRow(); - // Insert cells + // Insert book information row.insertCell().innerText = myLibrary[i].title; row.insertCell().innerText = myLibrary[i].author; row.insertCell().innerText = myLibrary[i].pages; // Read/Unread button - let readCell = row.insertCell(); + const readCell = row.insertCell(); - let changeBut = document.createElement("button"); + const changeBut = document.createElement("button"); changeBut.className = "btn btn-success"; changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; @@ -100,17 +115,23 @@ function render() { readCell.appendChild(changeBut); // Delete button - let deleteCell = row.insertCell(); + const deleteCell = row.insertCell(); - let delBut = document.createElement("button"); + const delBut = document.createElement("button"); delBut.className = "btn btn-warning"; delBut.innerText = "Delete"; delBut.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); + const deletedTitle = myLibrary[i].title; + // Delete the book first myLibrary.splice(i, 1); + + // Update the display render(); + + // Show confirmation after deletion + alert(`You've deleted title: ${deletedTitle}`); }); deleteCell.appendChild(delBut);