From 69e48578f99a519d04a21bec788e73a6dc03b3fe Mon Sep 17 00:00:00 2001 From: Oussama Mouggal Date: Sat, 4 Apr 2026 02:46:59 +0100 Subject: [PATCH 1/9] 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 b23cdb15a04c9bcdcf96cce648faceb7d5ff08af Mon Sep 17 00:00:00 2001 From: Oussama Mouggal Date: Sat, 4 Apr 2026 02:48:49 +0100 Subject: [PATCH 2/9] Fix reference to myLibrary --- 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 eed706fd..51a8c1cb 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -38,7 +38,7 @@ function submit() { return false; } else { let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); + myLibrary.push(book); render(); } } From ada8f3b2a3530bd25ffcf0b105e87d89c9d1902a Mon Sep 17 00:00:00 2001 From: Oussama Mouggal Date: Sat, 4 Apr 2026 02:53:53 +0100 Subject: [PATCH 3/9] Add delete button functionality to book entries in render --- debugging/book-library/script.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 51a8c1cb..e4adc6a1 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -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("clicks", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From 672e6f2928ad2f9cb1dfbea2dd89d0242d8bfcdb Mon Sep 17 00:00:00 2001 From: Oussama Mouggal Date: Sat, 4 Apr 2026 02:54:41 +0100 Subject: [PATCH 4/9] fix(book-library): validate author field before submit --- debugging/book-library/script.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index e4adc6a1..626a3bb7 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -30,7 +30,9 @@ const check = document.getElementById("check"); function submit() { if ( title.value == null || - title.value == "" || + title.value.trim() == "" || + author.value == null || + author.value.trim() == "" || pages.value == null || pages.value == "" ) { From 63a123e57400a06d5c1b6c7cc2fca3e596c7a301 Mon Sep 17 00:00:00 2001 From: Oussama Mouggal Date: Sat, 4 Apr 2026 02:55:05 +0100 Subject: [PATCH 5/9] fix(book-library): correct author field assignment in book creation --- 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 626a3bb7..630a9d53 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -39,7 +39,7 @@ function submit() { alert("Please fill all fields!"); return false; } else { - let book = new Book(title.value, title.value, pages.value, check.checked); + let book = new Book(title.value, author.value, pages.value, check.checked); myLibrary.push(book); render(); } From c68c93a93a2af24a7c21cb85eb593c372ded75a2 Mon Sep 17 00:00:00 2001 From: Oussama Mouggal Date: Sat, 4 Apr 2026 02:57:01 +0100 Subject: [PATCH 6/9] fix(book-library): correct read label and delete click handler --- debugging/book-library/script.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 630a9d53..8f8d714d 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -79,9 +79,9 @@ function render() { wasReadCell.appendChild(changeBut); let readStatus = ""; if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { readStatus = "No"; + } else { + readStatus = "Yes"; } changeBut.innerText = readStatus; @@ -96,7 +96,7 @@ function render() { deleteCell.appendChild(delButton); delButton.className = "btn btn-warning"; delButton.innerHTML = "Delete"; - delButton.addEventListener("clicks", function () { + delButton.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From 00e796e24a0715114268826f1c0aee2a9ca36441 Mon Sep 17 00:00:00 2001 From: Oussama Mouggal Date: Sun, 5 Apr 2026 14:35:58 +0100 Subject: [PATCH 7/9] Update HTML structure, improve input validation, and enhance rendering logic --- debugging/book-library/index.html | 24 +++----- debugging/book-library/script.js | 93 +++++++++++++++++-------------- debugging/book-library/style.css | 2 +- 3 files changed, 62 insertions(+), 57 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..fe1d9403 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 /> Library value="" />Read - +
@@ -91,6 +85,6 @@

Library

- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 8f8d714d..dccae7ce 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,47 +1,62 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +const titleInputEl = document.getElementById("title"); +const authorInputEl = document.getElementById("author"); +const pagesInputEl = document.getElementById("pages"); +const isReadInputEl = document.getElementById("check"); +const submitBookBtnEl = document.getElementById("submit-book-btn"); +const displayTableEl = document.getElementById("display"); + +window.addEventListener("load", function () { populateStorage(); render(); }); +submitBookBtnEl.addEventListener("click", submit); + function populateStorage() { - if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); + 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", + 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 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() { + const normalizedTitle = titleInputEl.value.trim(); + const normalizedAuthor = authorInputEl.value.trim(); + const pagesCount = Number(pagesInputEl.value); + if ( - title.value == null || - title.value.trim() == "" || - author.value == null || - author.value.trim() == "" || - pages.value == null || - pages.value == "" + normalizedTitle === "" || + normalizedAuthor === "" || + !Number.isInteger(pagesCount) || + pagesCount <= 0 ) { alert("Please fill all fields!"); return false; } else { - let book = new Book(title.value, author.value, pages.value, check.checked); + let book = new Book( + normalizedTitle, + normalizedAuthor, + pagesCount, + isReadInputEl.checked + ); myLibrary.push(book); render(); + + titleInputEl.value = ""; + authorInputEl.value = ""; + pagesInputEl.value = ""; + isReadInputEl.checked = false; } } @@ -53,53 +68,49 @@ 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--) { - table.deleteRow(n); - } + const tableBodyEl = displayTableEl.tBodies[0]; + tableBodyEl.replaceChildren(); + //insert updated row and cells let length = myLibrary.length; for (let i = 0; i < length; i++) { - let row = table.insertRow(1); + let row = tableBodyEl.insertRow(); 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; + titleCell.textContent = myLibrary[i].title; + authorCell.textContent = myLibrary[i].author; + pagesCell.textContent = String(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 toggleReadButton = document.createElement("button"); + toggleReadButton.className = "btn btn-success"; + wasReadCell.appendChild(toggleReadButton); let readStatus = ""; - if (myLibrary[i].check == false) { + if (myLibrary[i].check === false) { readStatus = "No"; } else { readStatus = "Yes"; } - changeBut.innerText = readStatus; + toggleReadButton.textContent = readStatus; - changeBut.addEventListener("click", function () { + toggleReadButton.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 () { - alert(`You've deleted title: ${myLibrary[i].title}`); + let deleteButton = document.createElement("button"); + deleteCell.appendChild(deleteButton); + deleteButton.className = "btn btn-warning"; + deleteButton.textContent = "Delete"; + deleteButton.addEventListener("click", function () { + const deletedTitle = myLibrary[i].title; myLibrary.splice(i, 1); render(); + alert(`You've deleted title: ${deletedTitle}`); }); } } diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..2d40464b 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,7 +1,7 @@ .form-group { width: 400px; height: 300px; - align-self: left; + align-self: flex-start; padding-left: 20px; } From 28f6037867d2ea9d811efa40412a60611ca13e24 Mon Sep 17 00:00:00 2001 From: Oussama Mouggal Date: Tue, 7 Apr 2026 15:33:59 +0100 Subject: [PATCH 8/9] updated validation message --- debugging/book-library/script.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index dccae7ce..1df8b3c1 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -41,7 +41,9 @@ function submit() { !Number.isInteger(pagesCount) || pagesCount <= 0 ) { - alert("Please fill all fields!"); + alert( + "Please fill all fields correctly. Title and author are required, and page count must be a positive whole number." + ); return false; } else { let book = new Book( From b3aed33a054cee9981b0e5f82447401ef3c10733 Mon Sep 17 00:00:00 2001 From: Oussama Mouggal Date: Tue, 7 Apr 2026 15:35:51 +0100 Subject: [PATCH 9/9] simplify read status display logic in render function --- debugging/book-library/script.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 1df8b3c1..50bb6abc 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -90,13 +90,7 @@ function render() { let toggleReadButton = document.createElement("button"); toggleReadButton.className = "btn btn-success"; wasReadCell.appendChild(toggleReadButton); - let readStatus = ""; - if (myLibrary[i].check === false) { - readStatus = "No"; - } else { - readStatus = "Yes"; - } - toggleReadButton.textContent = readStatus; + toggleReadButton.textContent = myLibrary[i].check === false ? "No" : "Yes"; toggleReadButton.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check;