From 7373cdb3f86d0e5db200b1701035b5c5375db18c Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Wed, 20 Aug 2025 21:44:11 +0100 Subject: [PATCH 01/11] Fix book submission logic and improve render function; add live server settings --- .vscode/settings.json | 3 +++ debugging/book-library/script.js | 32 ++++++++++++++++---------------- 2 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..2f836120 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5503 +} diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..95f878fe 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -28,18 +28,18 @@ 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 == "" - ) { + if (!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); + let book = new Book(title.value, author.value, pages.value, check.checked); + myLibrary.push(book); render(); + //clear user input data after storing the information + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; } } @@ -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 @@ -77,9 +77,9 @@ function render() { wasReadCell.appendChild(changeBut); let readStatus = ""; if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { readStatus = "No"; + } else { + readStatus = "Yes"; } changeBut.innerText = readStatus; @@ -90,11 +90,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 3af917387d143ff609be00a15a0d6fbb80454131 Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Wed, 20 Aug 2025 21:58:49 +0100 Subject: [PATCH 02/11] Remove unnecessary render call in populateStorage function --- debugging/book-library/script.js | 1 - 1 file changed, 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 95f878fe..72a85f24 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -16,7 +16,6 @@ function populateStorage() { ); myLibrary.push(book1); myLibrary.push(book2); - render(); } } From e288ff5f21cace77848147ad1a65ebd835e2a441 Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Wed, 20 Aug 2025 22:02:21 +0100 Subject: [PATCH 03/11] Fix book page count type in populateStorage function --- 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 72a85f24..25984549 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -7,7 +7,7 @@ window.addEventListener("load", function (e) { function populateStorage() { if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); + let book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true); let book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", From 39444d7a8df7850b45581499917ce9e469f43708 Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Wed, 20 Aug 2025 22:56:01 +0100 Subject: [PATCH 04/11] Fix book pages value type in populateStorage function --- 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 25984549..3d1e0150 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -11,7 +11,7 @@ function populateStorage() { let book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", - "127", + 127, true ); myLibrary.push(book1); From dcd06c697f3d9d972370d4500516cfd5f3264ced Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Wed, 20 Aug 2025 23:20:41 +0100 Subject: [PATCH 05/11] Refactor submit function to use ternary statment. --- debugging/book-library/script.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 3d1e0150..236bcf49 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -27,19 +27,23 @@ 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.trim() || !author.value.trim() || !pages.value.trim()) { - alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, author.value, pages.value, check.checked); - myLibrary.push(book); - render(); - //clear user input data after storing the information - title.value = ""; - author.value = ""; - pages.value = ""; - check.checked = false; - } + return !title.value.trim() || !author.value.trim() || !pages.value.trim() + ? (alert("Please fill all fields!"), false) + : function () { + let book = new Book( + title.value, + author.value, + pages.value, + check.checked + ); + myLibrary.push(book); + render(); + //clear user input data after storing the information + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; + }; } function Book(title, author, pages, check) { From 584176d52095293cc10d6c1a2a3cbf1d988b07a5 Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Wed, 20 Aug 2025 23:33:40 +0100 Subject: [PATCH 06/11] Rename input variable constants for clarity --- 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 236bcf49..d4370b7a 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -19,10 +19,10 @@ function populateStorage() { } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const checkInput = 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 From 3809f6168584afaca998b7a8cb43d1c4bf1036d5 Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Thu, 21 Aug 2025 00:37:30 +0100 Subject: [PATCH 07/11] Refactor submit function to improve readability and use consistent variable names --- debugging/book-library/script.js | 67 ++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index d4370b7a..870cebc4 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -27,25 +27,32 @@ const checkInput = 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() { - return !title.value.trim() || !author.value.trim() || !pages.value.trim() - ? (alert("Please fill all fields!"), false) - : function () { - let book = new Book( - title.value, - author.value, - pages.value, - check.checked - ); - myLibrary.push(book); - render(); - //clear user input data after storing the information - title.value = ""; - author.value = ""; - pages.value = ""; - check.checked = false; - }; -} + if ( + !titleInput.value.trim() || + !authorInput.value.trim() || + !pagesInput.value.trim() + ) { + alert("Please fill all fields!"); + return false; + } else { + myLibrary.push( + new Book( + titleInput.value.trim(), + authorInput.value.trim(), + Number(pagesInput.value), + checkInput.checked + ) + ); + render(); + //clear user input data after storing the information + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + checkInput.checked = false; + return true; + } +} function Book(title, author, pages, check) { this.title = title; this.author = author; @@ -74,30 +81,30 @@ function render() { 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 toggleReadBTN = document.createElement("button"); + toggleReadBTN.id = i; + toggleReadBTN.className = "btn btn-success"; + wasReadCell.appendChild(toggleReadBTN); let readStatus = ""; if (myLibrary[i].check == false) { readStatus = "No"; } else { readStatus = "Yes"; } - changeBut.innerText = readStatus; + toggleReadBTN.innerText = readStatus; - changeBut.addEventListener("click", function () { + toggleReadBTN.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); //add delete button to every row and render again - let delButton = document.createElement("button"); - delButton.id = i + 5; - deleteCell.appendChild(delButton); - delButton.className = "btn btn-warning"; - delButton.innerHTML = "Delete"; - delButton.addEventListener("click", function () { + let deleteBtn = document.createElement("button"); + deleteBtn.id = i + 5; + deleteCell.appendChild(deleteBtn); + deleteBtn.className = "btn btn-warning"; + deleteBtn.innerHTML = "Delete"; + deleteBtn.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From d883baa075b50161006479660b46698f3df6873e Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Thu, 21 Aug 2025 00:42:35 +0100 Subject: [PATCH 08/11] Refactor read status assignment in render function to use ternary operator --- debugging/book-library/script.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 870cebc4..f11f12f2 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -85,12 +85,7 @@ function render() { toggleReadBTN.id = i; toggleReadBTN.className = "btn btn-success"; wasReadCell.appendChild(toggleReadBTN); - let readStatus = ""; - if (myLibrary[i].check == false) { - readStatus = "No"; - } else { - readStatus = "Yes"; - } + let readStatus = myLibrary[i].check ? "Yes" : "No"; toggleReadBTN.innerText = readStatus; toggleReadBTN.addEventListener("click", function () { From a1b43bd6667d9aaa7270f05c1128a1f8336ed942 Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Thu, 21 Aug 2025 10:27:35 +0100 Subject: [PATCH 09/11] Fix HTML structure and input types for title and author fields --- debugging/book-library/index.html | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..de831da7 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,9 @@ - + - - + My Book Library + + @@ -31,7 +28,7 @@

Library

Library /> Date: Thu, 21 Aug 2025 10:56:43 +0100 Subject: [PATCH 10/11] Refactor submit function for better performance and improve input validation --- debugging/book-library/script.js | 43 ++++++++++++++++---------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index f11f12f2..e1c33745 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -27,32 +27,31 @@ const checkInput = 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 ( - !titleInput.value.trim() || - !authorInput.value.trim() || - !pagesInput.value.trim() - ) { + const titleValue = titleInput.value.trim(); + const authorValue = authorInput.value.trim(); + const pagesValue = pagesInput.value.trim(); + const pagesNumber = Number(pagesValue); + const isRead = checkInput.checked; + if (!titleValue || !authorValue || !pagesValue) { alert("Please fill all fields!"); return false; - } else { - myLibrary.push( - new Book( - titleInput.value.trim(), - authorInput.value.trim(), - Number(pagesInput.value), - checkInput.checked - ) - ); - - render(); - //clear user input data after storing the information - titleInput.value = ""; - authorInput.value = ""; - pagesInput.value = ""; - checkInput.checked = false; - return true; } + if (!Number.isInteger(pagesNumber) || pagesNumber <= 0) { + alert("Pages can be only whole number greater than 0!"); + return false; + } + + myLibrary.push(new Book(titleValue, authorValue, pagesNumber, isRead)); + + render(); + //clear user input data after storing the information + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + checkInput.checked = false; + return true; } + function Book(title, author, pages, check) { this.title = title; this.author = author; From e84f17c5829f1371659f34ee6b78eba142ac39e0 Mon Sep 17 00:00:00 2001 From: Mansoor Munawar Date: Thu, 21 Aug 2025 11:13:47 +0100 Subject: [PATCH 11/11] Refactor render function to improve button naming consistency and simplify event listener setup --- debugging/book-library/script.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index e1c33745..4c485df8 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -80,25 +80,23 @@ function render() { pagesCell.innerHTML = myLibrary[i].pages; //add and wait for action for read/unread button - let toggleReadBTN = document.createElement("button"); - toggleReadBTN.id = i; - toggleReadBTN.className = "btn btn-success"; - wasReadCell.appendChild(toggleReadBTN); + let toggleReadButton = document.createElement("button"); + toggleReadButton.className = "btn btn-success"; + wasReadCell.appendChild(toggleReadButton); let readStatus = myLibrary[i].check ? "Yes" : "No"; - toggleReadBTN.innerText = readStatus; + toggleReadButton.innerText = readStatus; - toggleReadBTN.addEventListener("click", function () { + toggleReadButton.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); //add delete button to every row and render again - let deleteBtn = document.createElement("button"); - deleteBtn.id = i + 5; - deleteCell.appendChild(deleteBtn); - deleteBtn.className = "btn btn-warning"; - deleteBtn.innerHTML = "Delete"; - deleteBtn.addEventListener("click", function () { + let deleteButton = document.createElement("button"); + deleteCell.appendChild(deleteButton); + deleteButton.className = "btn btn-warning"; + deleteButton.innerHTML = "Delete"; + deleteButton.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render();