From c4ec8a3a29c70f735fe48da7e9cfc8eceaf7a204 Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Fri, 7 Aug 2026 15:55:18 +0100 Subject: [PATCH 01/10] Fix JavaScript syntax errors and delete button functionality --- debugging/book-library/script.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..d38bae94 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 @@ -89,12 +89,12 @@ function render() { }); //add delete button to every row and render again - let delButton = document.createElement("button"); - delBut.id = i + 5; + let delBut = document.createElement("button"); + delBut.id = i; deleteCell.appendChild(delBut); delBut.className = "btn btn-warning"; delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From 6b461b00e1f73f03f26e6719a0f15d8f527373e0 Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Fri, 7 Aug 2026 16:00:36 +0100 Subject: [PATCH 02/10] Fix adding new books to the library --- 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 d38bae94..3c77bc8b 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -37,8 +37,8 @@ function submit() { 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(); } } From 75edbaaba3d1f90c2ebbea344d27b6a66f3ad0f4 Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Fri, 7 Aug 2026 17:16:03 +0100 Subject: [PATCH 03/10] Fix book validation and improve rendering --- debugging/book-library/index.html | 6 ++--- debugging/book-library/script.js | 45 +++++++++++++++++++------------ 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..5011c879 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,4 +1,4 @@ - + @@ -57,7 +57,7 @@

Library

Read @@ -65,7 +65,7 @@

Library

type="submit" value="Submit" class="btn btn-primary" - onclick="submit();" + onclick="submit()" /> diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 3c77bc8b..c8d24e1d 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -6,7 +6,7 @@ window.addEventListener("load", function (e) { }); function populateStorage() { - if (myLibrary.length == 0) { + if (myLibrary.length === 0) { let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); let book2 = new Book( "The Old Man and the Sea", @@ -23,31 +23,42 @@ function populateStorage() { const title = document.getElementById("title"); const author = document.getElementById("author"); const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +const isRead = document.getElementById("isRead"); //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 == "" + title.value === null || + title.value === "" || + author.value === null || + author.value === "" || + pages.value === null || + pages.value === "" ) { alert("Please fill all fields!"); return false; } else { - let book = new Book(title.value, author.value, pages.value, check.checked); + let book = new Book(title.value, author.value, pages.value, isRead.checked); myLibrary.push(book); + clearForm(); + render(); } } -function Book(title, author, pages, check) { +function clearForm() { + title.value = ""; + author.value = ""; + pages.value = ""; + isRead.checked = false; +} + +function Book(title, author, pages, isRead) { this.title = title; this.author = author; this.pages = pages; - this.check = check; + this.isRead = isRead; } function render() { @@ -66,9 +77,9 @@ function render() { 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; + titleCell.textContent = myLibrary[i].title; + authorCell.textContent = myLibrary[i].author; + pagesCell.textContent = myLibrary[i].pages; //add and wait for action for read/unread button let changeBut = document.createElement("button"); @@ -76,15 +87,15 @@ function render() { changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { + if (myLibrary[i].isRead === false) { readStatus = "No"; + } else { + readStatus = "Yes"; } changeBut.innerText = readStatus; changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + myLibrary[i].isRead = !myLibrary[i].isRead; render(); }); @@ -93,7 +104,7 @@ function render() { delBut.id = i; deleteCell.appendChild(delBut); delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; + delBut.innerText = "Delete"; delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); From f56e07a826104741d093080dc2603104b4de21af Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Fri, 7 Aug 2026 17:21:45 +0100 Subject: [PATCH 04/10] Fix library bugs and improve book management --- debugging/book-library/index.html | 2 +- debugging/book-library/script.js | 11 ++--------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 5011c879..985b80ef 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -65,7 +65,7 @@

Library

type="submit" value="Submit" class="btn btn-primary" - onclick="submit()" + onclick="addBook()" /> diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index c8d24e1d..0be63bad 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -27,15 +27,8 @@ const isRead = document.getElementById("isRead"); //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 === "" || - author.value === null || - author.value === "" || - pages.value === null || - pages.value === "" - ) { +function addBook() { + if (!title.value || !author.value || !pages.value) { alert("Please fill all fields!"); return false; } else { From cbf351d4dc72f9cacd066515eb2cd4a4f00b3c5f Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Fri, 7 Aug 2026 17:29:19 +0100 Subject: [PATCH 05/10] Refactor library DOM handling and improve readability --- debugging/book-library/index.html | 2 +- debugging/book-library/script.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 985b80ef..9a46fe60 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -80,7 +80,7 @@

Library

- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 0be63bad..a466ecba 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -55,16 +55,16 @@ function Book(title, author, pages, isRead) { } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; + let tableBody = document.querySelector("#display tbody"); + let rowsNumber = tableBody.rows.length; //delete old table for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); + tableBody.deleteRow(n); } //insert updated row and cells let length = myLibrary.length; for (let i = 0; i < length; i++) { - let row = table.insertRow(1); + let row = tableBody.insertRow(1); let titleCell = row.insertCell(0); let authorCell = row.insertCell(1); let pagesCell = row.insertCell(2); From 63d90e21425c856acf9ab76533824fe2d244a88b Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Fri, 7 Aug 2026 17:31:06 +0100 Subject: [PATCH 06/10] Fix library bugs and improve book management --- debugging/book-library/index.html | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 9a46fe60..739f423d 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -80,15 +80,7 @@

Library

- - - - - - - - - + From 4392a4128df26ed78835053ab2e8281850c5de56 Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Sat, 8 Aug 2026 01:59:19 +0100 Subject: [PATCH 07/10] Improve HTML validation and form handling --- debugging/book-library/index.html | 87 +++++++++++---------- debugging/book-library/script.js | 124 +++++++++++++++--------------- 2 files changed, 109 insertions(+), 102 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 739f423d..62913aed 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -10,6 +10,7 @@ + Library
-
- - - - - - -
+
@@ -80,9 +84,10 @@

Library

- + +
- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index a466ecba..11d623c7 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,50 +1,59 @@ 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( + const book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true); + const book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", - "127", + 127, true ); + myLibrary.push(book1); myLibrary.push(book2); - render(); } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const isRead = document.getElementById("isRead"); +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const isReadInput = document.getElementById("isRead"); +const bookForm = document.getElementById("book-form"); + +bookForm.addEventListener("submit", function (event) { + event.preventDefault(); + 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!"); - return false; - } else { - let book = new Book(title.value, author.value, pages.value, isRead.checked); - myLibrary.push(book); - clearForm(); - - render(); + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + const pages = Number.parseInt(pagesInput.value, 10); + + if (!title || !author || !Number.isInteger(pages) || pages < 1) { + alert("Please enter valid book details."); + return; } + + const book = new Book(title, author, pages, isReadInput.checked); + + myLibrary.push(book); + + clearForm(); + render(); } function clearForm() { - title.value = ""; - author.value = ""; - pages.value = ""; - isRead.checked = false; + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + isReadInput.checked = false; } function Book(title, author, pages, isRead) { @@ -55,53 +64,46 @@ function Book(title, author, pages, isRead) { } function render() { - let tableBody = document.querySelector("#display tbody"); - let rowsNumber = tableBody.rows.length; - //delete old table - for (let n = rowsNumber - 1; n > 0; n--) { - tableBody.deleteRow(n); - } - //insert updated row and cells - let length = myLibrary.length; - for (let i = 0; i < length; i++) { - let row = tableBody.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); + const tableBody = document.getElementById("book-list"); + + tableBody.replaceChildren(); + + for (let i = 0; i < myLibrary.length; i++) { + const row = tableBody.insertRow(); + const titleCell = row.insertCell(); + const authorCell = row.insertCell(); + const pagesCell = row.insertCell(); + const wasReadCell = row.insertCell(); + const deleteCell = row.insertCell(); + titleCell.textContent = myLibrary[i].title; authorCell.textContent = myLibrary[i].author; pagesCell.textContent = 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].isRead === false) { - readStatus = "No"; - } else { - readStatus = "Yes"; - } - changeBut.innerText = readStatus; - - changeBut.addEventListener("click", function () { + const changeButton = document.createElement("button"); + changeButton.className = "btn btn-success"; + changeButton.textContent = myLibrary[i].isRead ? "No" : "Yes"; + + wasReadCell.appendChild(changeButton); + + changeButton.addEventListener("click", function () { myLibrary[i].isRead = !myLibrary[i].isRead; render(); }); - //add delete button to every row and render again - let delBut = document.createElement("button"); - delBut.id = i; - deleteCell.appendChild(delBut); - delBut.className = "btn btn-warning"; - delBut.innerText = "Delete"; - delBut.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); + const deleteButton = document.createElement("button"); + deleteButton.className = "btn btn-warning"; + deleteButton.textContent = "Delete"; + + deleteCell.appendChild(deleteButton); + + deleteButton.addEventListener("click", function () { + const deletedTitle = myLibrary[i].title; + myLibrary.splice(i, 1); render(); + + alert(`You've deleted title: ${deletedTitle}`); }); } } From 0fe35f832ce928c2a70ebaeb956380356344d691 Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Sun, 9 Aug 2026 14:19:38 +0100 Subject: [PATCH 08/10] Refine library initialization and form handling --- debugging/book-library/script.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 11d623c7..a6b2c9f3 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,8 +1,18 @@ -let myLibrary = []; +const myLibrary = []; + +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const isReadInput = document.getElementById("isRead"); +const bookForm = document.getElementById("book-form"); window.addEventListener("load", function () { populateStorage(); render(); + bookForm.addEventListener("submit", function (event) { + event.preventDefault(); + addBook(); + }); }); function populateStorage() { @@ -20,16 +30,7 @@ function populateStorage() { } } -const titleInput = document.getElementById("title"); -const authorInput = document.getElementById("author"); -const pagesInput = document.getElementById("pages"); -const isReadInput = document.getElementById("isRead"); -const bookForm = document.getElementById("book-form"); -bookForm.addEventListener("submit", function (event) { - event.preventDefault(); - addBook(); -}); function addBook() { const title = titleInput.value.trim(); @@ -50,10 +51,7 @@ function addBook() { } function clearForm() { - titleInput.value = ""; - authorInput.value = ""; - pagesInput.value = ""; - isReadInput.checked = false; + bookForm.reset(); } function Book(title, author, pages, isRead) { @@ -103,7 +101,9 @@ function render() { myLibrary.splice(i, 1); render(); - alert(`You've deleted title: ${deletedTitle}`); + setTimeout(() => { + alert(`You've deleted title: ${deletedTitle}`); + }, 0); }); } } From 404db2dfc955561c76d06f786c74e695fc5f55ea Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Sun, 9 Aug 2026 14:41:23 +0100 Subject: [PATCH 09/10] Fix HTML validation and refine form handling --- debugging/book-library/index.html | 36 +++++++++++++------------------ 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 62913aed..cd58ee09 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,21 +1,15 @@ - + - - - - - - - - + + + + My Library + + + + + @@ -38,7 +32,7 @@

Library

id="title" name="title" required - /> + > Library id="author" name="author" required - /> + > Library name="pages" min="1" required - /> + > @@ -81,7 +75,7 @@

Library

Author Number of Pages Read - + Delete From b9b060b19e6435efc11e896ec62fdbc7b405aaea Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Sun, 9 Aug 2026 23:01:50 +0100 Subject: [PATCH 10/10] Restore Bootstrap JavaScript dependencies --- debugging/book-library/index.html | 28 +++++++++++++++++----------- debugging/book-library/script.js | 4 +--- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index cd58ee09..a1f815f0 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,15 +1,21 @@ - - - + + + My Library - + - - - + + + + + + @@ -32,7 +38,7 @@

Library

id="title" name="title" required - > + /> Library id="author" name="author" required - > + /> Library name="pages" min="1" required - > + /> diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index a6b2c9f3..8c8945f5 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -30,8 +30,6 @@ function populateStorage() { } } - - function addBook() { const title = titleInput.value.trim(); const author = authorInput.value.trim(); @@ -80,7 +78,7 @@ function render() { const changeButton = document.createElement("button"); changeButton.className = "btn btn-success"; - changeButton.textContent = myLibrary[i].isRead ? "No" : "Yes"; + changeButton.textContent = myLibrary[i].isRead ? "Yes" : "No"; wasReadCell.appendChild(changeButton);