From 4c8564b95acbe73cbe1b472e08b0137b294d2d79 Mon Sep 17 00:00:00 2001 From: halimatou-saddiyaa Date: Tue, 29 Jul 2025 21:15:44 +0100 Subject: [PATCH 1/3] Correcting bugs in html file --- debugging/book-library/index.html | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..eca06049 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -37,14 +37,16 @@

Library

name="title" required /> + + Library Author Number of Pages Read - + Delete - + From 5abd596eeef9832b3a8d20a7bc3258796428c3ef Mon Sep 17 00:00:00 2001 From: halimatou-saddiyaa Date: Tue, 29 Jul 2025 21:16:19 +0100 Subject: [PATCH 2/3] Correcting bugs in script.js --- debugging/book-library/script.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..c0278ee1 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("Robinson Crusoe", "Daniel Defoe", "252", true); // Correcting the book's name to Robinson instead of Robison let book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", @@ -16,7 +16,7 @@ function populateStorage() { ); myLibrary.push(book1); myLibrary.push(book2); - render(); + // removing render() here as it is already called in line 5 } } @@ -31,14 +31,16 @@ function submit() { if ( title.value == null || title.value == "" || + author.value == null || // adding author to the validation + author.value == "" || pages.value == null || 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); + let book = new Book(title.value, author.value, pages.value, check.checked); // Correcting the repetition of title.value and replacing it with author.value + myLibrary.push(book); // Correcting with the right function name render(); } } @@ -54,7 +56,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--) { // Adding a missing parenthesis table.deleteRow(n); } //insert updated row and cells @@ -73,10 +75,17 @@ function render() { //add and wait for action for read/unread button let changeBut = document.createElement("button"); changeBut.id = i; + + // Adding option of btn-danger if status is not read + if (myLibrary[i].check === true) { changeBut.className = "btn btn-success"; + } else { + changeBut.className = "btn btn-danger"; +} + wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == false) { + if (myLibrary[i].check === true) { // Changing the condition from false to true to validate the condition yes and using === instead of == to check for value and type readStatus = "Yes"; } else { readStatus = "No"; @@ -89,12 +98,12 @@ function render() { }); //add delete button to every row and render again - let delButton = document.createElement("button"); + let delBut = document.createElement("button"); // Correcting the function name from delButton to delBut to match following code delBut.id = i + 5; deleteCell.appendChild(delBut); delBut.className = "btn btn-warning"; delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delBut.addEventListener("click", function () { // correcting using the correct term: click instead of clicks alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From 6723b0158a71e05c7f123043ccc813aa2ed54d8e Mon Sep 17 00:00:00 2001 From: halimatou-saddiyaa Date: Thu, 7 Aug 2025 01:16:56 +0200 Subject: [PATCH 3/3] changed code to prevent adding negative page numbers and added an alert before deleting a book --- debugging/book-library/script.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index c0278ee1..d17da11b 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -38,12 +38,19 @@ function submit() { ) { alert("Please fill all fields!"); return false; - } else { - let book = new Book(title.value, author.value, pages.value, check.checked); // Correcting the repetition of title.value and replacing it with author.value + } + + const pagesNumber = Number(pages.value); + if (isNaN(pagesNumber) || pagesNumber <= 0) { + alert("Please enter a valid, positive number of pages."); + return false; + } + + let book = new Book(title.value, author.value, pagesNumber, check.checked); // Correcting the repetition of title.value and replacing it with author.value myLibrary.push(book); // Correcting with the right function name render(); } -} + function Book(title, author, pages, check) { this.title = title; @@ -104,9 +111,12 @@ function render() { delBut.className = "btn btn-warning"; delBut.innerHTML = "Delete"; delBut.addEventListener("click", function () { // correcting using the correct term: click instead of clicks + const confirmDelete = confirm(`Are you sure you want to delete "${myLibrary[i].title}"?`); + if (confirmDelete) { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); + } }); } }