From 14c327ca69558bd259ef1f816cf0d0d1e30df0f5 Mon Sep 17 00:00:00 2001 From: Rizqah Date: Thu, 13 Aug 2026 18:35:19 +0100 Subject: [PATCH 1/6] book library bugs fixed --- debugging/book-library/index.html | 28 +++---- debugging/book-library/script.js | 131 +++++++++++++++++------------- debugging/book-library/style.css | 13 +++ 3 files changed, 102 insertions(+), 70 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71e..f1b5d745c 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,20 +1,20 @@ - + - + Rizqah's Book Library + + > - + > + @@ -31,20 +31,20 @@

Library

+ > + > Library id="pages" name="pages" required - /> + > + >
- +

diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d3..e076a3671 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,46 +1,58 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const checkInput = document.getElementById("check"); + +function setup() { populateStorage(); render(); -}); +} function populateStorage() { - if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); - let book2 = new Book( + if (myLibrary.length === 0) { + const book1 = new Book("Robison 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 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 == "" + titleInput.value.trim() === "" || + authorInput.value.trim() === "" || + pagesInput.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(); } + + const pages = Number(pagesInput.value); + + if (pages <= 0 || !Number.isInteger(pages)) { + alert("Please enter a valid page number."); + return false; + } + + const book = new Book( + titleInput.value.trim(), + authorInput.value.trim(), + pages, + checkInput.checked + ); + + myLibrary.push(book); + render(); } function Book(title, author, pages, check) { @@ -51,53 +63,60 @@ 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 tableBody = document.querySelector("#display tbody"); + + //delete old table rows + tableBody.innerHTML = ""; + //insert updated row and cells - let length = myLibrary.length; + const 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; + const row = tableBody.insertRow(); + + const titleCell = row.insertCell(0); + const authorCell = row.insertCell(1); + const pagesCell = row.insertCell(2); + const wasReadCell = row.insertCell(3); + const deleteCell = row.insertCell(4); + + 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].check == false) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; - - changeBut.addEventListener("click", function () { + const changeButton = document.createElement("button"); + changeButton.className = "btn btn-success"; + wasReadCell.appendChild(changeButton); + + const readStatus = myLibrary[i].check ? "Yes" : "No"; + changeButton.textContent = readStatus; + + changeButton.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); - delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", 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 bookTitle = myLibrary[i].title; + myLibrary.splice(i, 1); render(); + + const message = document.getElementById("message"); + message.textContent = `You've deleted title: ${bookTitle}`; + + setTimeout(function () { + message.textContent = ""; + }, 3000); }); } } + +window.addEventListener("load", setup); diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb8..babcbc60d 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -17,3 +17,16 @@ button.btn-info { margin: 20px; } +#message { + margin: 15px 0; + padding: 0; + color: #155724; + font-weight: 500; +} + +#message:not(:empty) { + padding: 12px 16px; + background-color: #d4edda; + border-left: 4px solid #28a745; + border-radius: 4px; +} From 2789a2dabaa7dd67ff59dc7bc637a9d00b6772e5 Mon Sep 17 00:00:00 2001 From: Rizqah Date: Sun, 16 Aug 2026 22:20:00 +0100 Subject: [PATCH 2/6] pages input field made to only accept whole number and checkbox now visible --- debugging/book-library/index.html | 16 ++++++++-------- debugging/book-library/style.css | 6 ------ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index f1b5d745c..5995d635b 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -51,21 +51,21 @@

Library

class="form-control" id="pages" name="pages" + min="1" + step="1" required > -
From 7c477d77b994b89cde5fc04ca3e1ca715f0e45ed Mon Sep 17 00:00:00 2001 From: Rizqah Date: Mon, 17 Aug 2026 03:32:29 +0100 Subject: [PATCH 5/6] added bookform event listener is the appropriate place. --- 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 c427a992d..c57a68080 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -8,6 +8,8 @@ const checkInput = document.getElementById("check"); function setup() { populateStorage(); render(); + const bookForm = document.getElementById("book-form"); + bookForm.addEventListener("submit", addBook); } function populateStorage() { @@ -27,7 +29,8 @@ function populateStorage() { //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() { +function addBook(event) { + event.preventDefault(); const titleValue = titleInput.value.trim(); const authorValue = authorInput.value.trim(); const pagesValue = pagesInput.value; @@ -77,9 +80,6 @@ function render() { titleCell.textContent = myLibrary[i].title; authorCell.textContent = myLibrary[i].author; pagesCell.textContent = myLibrary[i].pages; - const submitButton = document.getElementById("submit"); - - submitButton.addEventListener("click", submit); //add and wait for action for read/unread button const changeButton = document.createElement("button"); From 28120ac5187756fc3bfe75f42cfd87c44a1571d1 Mon Sep 17 00:00:00 2001 From: rizqahthetechgirlie Date: Wed, 19 Aug 2026 20:36:56 +0100 Subject: [PATCH 6/6] bookform event listeners out of setup --- 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 c57a68080..529d0713b 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -4,12 +4,12 @@ const titleInput = document.getElementById("title"); const authorInput = document.getElementById("author"); const pagesInput = document.getElementById("pages"); const checkInput = document.getElementById("check"); +const bookForm = document.getElementById("book-form"); function setup() { populateStorage(); render(); - const bookForm = document.getElementById("book-form"); - bookForm.addEventListener("submit", addBook); + } function populateStorage() { @@ -115,5 +115,5 @@ function render() { }); } } - +bookForm.addEventListener("submit", addBook); window.addEventListener("load", setup);