From dde250be588255dcadf91f0de88e52a591f06749 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Tue, 16 Dec 2025 14:46:27 +0000 Subject: [PATCH 01/19] a full code analysis with some notes. fix the wrong typo of variable name (delBtutton) and close brackets for if statement --- 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 75ce6c1d..deb4a0b8 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,12 +1,13 @@ let myLibrary = []; window.addEventListener("load", function (e) { - populateStorage(); + populateStorage(); // just if myLibrary is empty put two book on it with a specific details render(); }); +//just one thing need to check function populateStorage() { - if (myLibrary.length == 0) { + if (myLibrary.length == 0) { //why there is just == not === let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); let book2 = new Book( "The Old Man and the Sea", @@ -19,8 +20,8 @@ function populateStorage() { render(); } } - -const title = document.getElementById("title"); +// clear associated with HTML +const title = document.getElementById("title"); const author = document.getElementById("author"); const pages = document.getElementById("pages"); const check = document.getElementById("check"); @@ -28,22 +29,26 @@ 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 any of the information is missing show an alert if ( - title.value == null || - title.value == "" || - pages.value == null || + title.value == null || // == checks both null and undefined . === only null + title.value == "" || // why you use the way and where author check?? + pages.value == null || // if(!title.value||author.value|| page.value) is more clear and readable pages.value == "" - ) { - alert("Please fill all fields!"); + ) + { + alert("Please fill all fields!"); // alert is a built-in JS function return false; } else { let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); - render(); + library.push(book); // ???? the var called MyLibrary bro + render(); // tell now where as submit event maybe later I will check } } + let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); ///why i have this in populteStorage and here -function Book(title, author, pages, check) { +//function to create a new book as an object + function Book(title, author, pages, check) { this.title = title; this.author = author; this.pages = pages; @@ -54,50 +59,52 @@ 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); + for (let n = rowsNumber - 1; n > 0; n--) { //n>0 because we will delete all rows except the first one header + table.deleteRow(n); //.deleteRow is a built-in JS function it remove the row by index } //insert updated row and cells let length = myLibrary.length; for (let i = 0; i < length; i++) { - let row = table.insertRow(1); - let titleCell = row.insertCell(0); + let row = table.insertRow(1); // why (1) not i+1 insert with four + let titleCell = row.insertCell(0); // the table.rows and row.cells are HTMLCollection,not a real array, but they behave like array(indexed,length) . they are DOM collections let authorCell = row.insertCell(1); - let pagesCell = row.insertCell(2); + let pagesCell = row.insertCell(2); let wasReadCell = row.insertCell(3); - let deleteCell = row.insertCell(4); - titleCell.innerHTML = myLibrary[i].title; + let deleteCell = row.insertCell(4); // it is like insert for + titleCell.innerHTML = myLibrary[i].title; //filling the cells why innerHTML authorCell.innerHTML = myLibrary[i].author; pagesCell.innerHTML = myLibrary[i].pages; //add and wait for action for read/unread button let changeBut = document.createElement("button"); - changeBut.id = i; + changeBut.id = i; // give the button the index of the book object changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); + wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == false) { + if (myLibrary[i].check == false) { // why not === readStatus = "Yes"; } else { readStatus = "No"; } - changeBut.innerText = readStatus; + changeBut.innerText = readStatus; // fill the button with yes or no changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + myLibrary[i].check = !myLibrary[i].check; //when we click will change yes to no and no to yes render(); }); - //add delete button to every row and render again + //add delete button to every row and render again + //delButton 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; // I think there is an issue here if we have more than five books + 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(); + //we do rendering after each change to UI }); } } From 4ec39f32fd3904b755fd71f82b4c7df88418d206 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Tue, 16 Dec 2025 14:51:39 +0000 Subject: [PATCH 02/19] website load bug has been solved --- debugging/book-library/script.js | 1 + 1 file changed, 1 insertion(+) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index deb4a0b8..6812ba40 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -108,3 +108,4 @@ function render() { }); } } + From 174123945cd99461253905b35fe4c4498b55d19d Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Tue, 16 Dec 2025 14:55:52 +0000 Subject: [PATCH 03/19] correct the var name --- 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 6812ba40..798f8d35 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -41,7 +41,7 @@ function submit() { return false; } else { let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); // ???? the var called MyLibrary bro + myLibrary.push(book); // ???? the var called MyLibrary bro render(); // tell now where as submit event maybe later I will check } } From 73d22f97ddb0f2d39f503edf29c7c6420956af21 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Tue, 16 Dec 2025 14:59:40 +0000 Subject: [PATCH 04/19] Console Error and author name have been solved --- 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 798f8d35..9ddbf811 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -40,7 +40,7 @@ function submit() { alert("Please fill all fields!"); // alert is a built-in JS function 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); // ???? the var called MyLibrary bro render(); // tell now where as submit event maybe later I will check } From fa0089f546b4edb2a5f09ce8266f737533fd9b1d Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Tue, 16 Dec 2025 15:01:59 +0000 Subject: [PATCH 05/19] addEventListener bug has been solved --- 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 9ddbf811..029a7655 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -100,7 +100,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 48c8f6ceb113bcd7d6d1ccd667a3fd959da85ab4 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Tue, 16 Dec 2025 15:28:52 +0000 Subject: [PATCH 06/19] all bugs to be fixed have been solved --- 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 029a7655..2e412704 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -38,7 +38,7 @@ function submit() { ) { alert("Please fill all fields!"); // alert is a built-in JS function - return false; + return false; } else { let book = new Book(title.value, author.value, pages.value, check.checked); myLibrary.push(book); // ???? the var called MyLibrary bro @@ -81,7 +81,7 @@ function render() { changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == false) { // why not === + if (myLibrary[i].check == true) { // why not === readStatus = "Yes"; } else { readStatus = "No"; From 09081ddef151307ce7ebf431fe05a0e9d592b857 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Tue, 16 Dec 2025 15:33:46 +0000 Subject: [PATCH 07/19] refactor and add author validtion --- debugging/book-library/script.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 2e412704..1b546cbd 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -30,12 +30,11 @@ const check = document.getElementById("check"); //via Book function and start render function function submit() { //if any of the information is missing show an alert - if ( - title.value == null || // == checks both null and undefined . === only null - title.value == "" || // why you use the way and where author check?? - pages.value == null || // if(!title.value||author.value|| page.value) is more clear and readable - pages.value == "" - ) + if (!title.value || !author.value ||!pages.value) + // == checks both null and undefined . === only null + // why you use this way and where author check?? + // if(!title.value||author.value|| page.value) is more clear and readable + { alert("Please fill all fields!"); // alert is a built-in JS function return false; From febabaf9d0eeebc8a4b54482404eef04928eeefa Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Tue, 16 Dec 2025 15:50:17 +0000 Subject: [PATCH 08/19] clean the code --- debugging/book-library/script.js | 48 ++++++++++++++------------------ 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 1b546cbd..fca8ff15 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,13 +1,13 @@ let myLibrary = []; window.addEventListener("load", function (e) { - populateStorage(); // just if myLibrary is empty put two book on it with a specific details + populateStorage(); render(); }); //just one thing need to check function populateStorage() { - if (myLibrary.length == 0) { //why there is just == not === + if (myLibrary.length === 0) { //why there is just == not === let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); let book2 = new Book( "The Old Man and the Sea", @@ -20,8 +20,8 @@ function populateStorage() { render(); } } -// clear associated with HTML -const title = document.getElementById("title"); + +const title = document.getElementById("title"); const author = document.getElementById("author"); const pages = document.getElementById("pages"); const check = document.getElementById("check"); @@ -30,24 +30,18 @@ const check = document.getElementById("check"); //via Book function and start render function function submit() { //if any of the information is missing show an alert - if (!title.value || !author.value ||!pages.value) - // == checks both null and undefined . === only null - // why you use this way and where author check?? - // if(!title.value||author.value|| page.value) is more clear and readable - - { - alert("Please fill all fields!"); // alert is a built-in JS function - return false; + 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, check.checked); - myLibrary.push(book); // ???? the var called MyLibrary bro - render(); // tell now where as submit event maybe later I will check + myLibrary.push(book); + render(); } } - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); ///why i have this in populteStorage and here -//function to create a new book as an object - function Book(title, author, pages, check) { +function Book(title, author, pages, check) { this.title = title; this.author = author; this.pages = pages; @@ -58,8 +52,8 @@ function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; //delete old table - for (let n = rowsNumber - 1; n > 0; n--) { //n>0 because we will delete all rows except the first one header - table.deleteRow(n); //.deleteRow is a built-in JS function it remove the row by index + for (let n = rowsNumber - 1; n > 0; n--) { + table.deleteRow(n); } //insert updated row and cells let length = myLibrary.length; @@ -67,7 +61,7 @@ function render() { let row = table.insertRow(1); // why (1) not i+1 insert with four let titleCell = row.insertCell(0); // the table.rows and row.cells are HTMLCollection,not a real array, but they behave like array(indexed,length) . they are DOM collections let authorCell = row.insertCell(1); - let pagesCell = row.insertCell(2); + let pagesCell = row.insertCell(2); let wasReadCell = row.insertCell(3); let deleteCell = row.insertCell(4); // it is like insert for titleCell.innerHTML = myLibrary[i].title; //filling the cells why innerHTML @@ -78,24 +72,24 @@ function render() { let changeBut = document.createElement("button"); changeBut.id = i; // give the button the index of the book object changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); + wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == true) { // why not === + if (myLibrary[i].check === true) { readStatus = "Yes"; } else { readStatus = "No"; } - changeBut.innerText = readStatus; // fill the button with yes or no + changeBut.innerText = readStatus; changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; //when we click will change yes to no and no to yes + myLibrary[i].check = !myLibrary[i].check; render(); }); //add delete button to every row and render again - //delButton + let delButton = document.createElement("button"); - delButton.id = i + 5; // I think there is an issue here if we have more than five books + delButton.id = i + 5; deleteCell.appendChild(delButton); delButton.className = "btn btn-warning"; delButton.innerHTML = "Delete"; @@ -103,7 +97,7 @@ function render() { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); - //we do rendering after each change to UI + }); } } From 8b9191784decac10df2ff014e9fcedb6d1b5c18e Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Wed, 17 Dec 2025 20:29:24 +0000 Subject: [PATCH 09/19] fix the error in index file add type attribute (module) remove onclick and add id with event listener instead. Check validated --- debugging/book-library/index.html | 13 ++++++------- debugging/book-library/script.js | 17 +++++++++++------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..4fc91855 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,11 +1,10 @@ - + Book Library @@ -31,7 +30,7 @@

Library

Library /> Library type="submit" value="Submit" class="btn btn-primary" - onclick="submit();" + id="submitBtn" />
@@ -91,6 +90,6 @@

Library

- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index fca8ff15..eb47bee5 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,5 +1,13 @@ let myLibrary = []; +const title = document.getElementById("title"); +const author = document.getElementById("author"); +const pages = document.getElementById("pages"); +const check = document.getElementById("check"); + +const submitBtn = document.getElementById("submitBtn"); +submitBtn.addEventListener("click", submit); + window.addEventListener("load", function (e) { populateStorage(); render(); @@ -7,7 +15,7 @@ window.addEventListener("load", function (e) { //just one thing need to check function populateStorage() { - if (myLibrary.length === 0) { //why there is just == not === + if (myLibrary.length === 0) { let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); let book2 = new Book( "The Old Man and the Sea", @@ -17,19 +25,16 @@ function populateStorage() { ); 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 any of the information is missing show an alert + if (!title.value || !author.value || !pages.value) { alert("Please fill all fields!"); From a2b51063ba9b09bdd89d9b9752ffc816b8d6200c Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Wed, 17 Dec 2025 20:39:41 +0000 Subject: [PATCH 10/19] fix page count type --- debugging/book-library/script.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index eb47bee5..7441b947 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -16,11 +16,11 @@ window.addEventListener("load", function (e) { //just one thing need to check 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", - "127", + 127, true ); myLibrary.push(book1); @@ -28,8 +28,6 @@ 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() { @@ -37,10 +35,9 @@ function submit() { if (!title.value || !author.value || !pages.value) { alert("Please fill all fields!"); - - return false; + return ; } else { - let book = new Book(title.value, author.value, pages.value, check.checked); + let book = new Book(title.value, author.value,Number(pages.value), check.checked); myLibrary.push(book); render(); } From cd54fe967b86cef13a5fd57a852d2c9c2d0d43f8 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Wed, 17 Dec 2025 20:43:50 +0000 Subject: [PATCH 11/19] improve code readability --- debugging/book-library/script.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 7441b947..54243dca 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,9 +1,9 @@ let myLibrary = []; -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 isReadInput = document.getElementById("check"); const submitBtn = document.getElementById("submitBtn"); submitBtn.addEventListener("click", submit); @@ -33,11 +33,11 @@ function populateStorage() { function submit() { //if any of the information is missing show an alert - if (!title.value || !author.value || !pages.value) { + if (!titleInput.value || !authorInput.value || !pagesInput.value) { alert("Please fill all fields!"); return ; } else { - let book = new Book(title.value, author.value,Number(pages.value), check.checked); + let book = new Book(titleInput.value, authorInput.value,Number(pagesInput.value), isReadInput.checked); myLibrary.push(book); render(); } From b241b9bacd5c5cabb60e04b46eb5b5da68c2e612 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Wed, 17 Dec 2025 21:17:19 +0000 Subject: [PATCH 12/19] delete all rows in one operation --- debugging/book-library/index.html | 2 +- debugging/book-library/script.js | 43 +++++++++++++++++-------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 4fc91855..0d05f114 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -79,7 +79,7 @@

Library

- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 54243dca..e5c8dd55 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -13,9 +13,9 @@ window.addEventListener("load", function (e) { render(); }); -//just one thing need to check +//just one thing need to check 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", @@ -32,12 +32,19 @@ function populateStorage() { //via Book function and start render function function submit() { //if any of the information is missing show an alert - - if (!titleInput.value || !authorInput.value || !pagesInput.value) { + const titleValue = titleInput.value.trim(); + const authorValue = authorInput.value.trim(); + const pagesValue = Number(pagesInput.value); + if (!titleValue || !authorValue || !pagesValue || pagesValue < 0) { alert("Please fill all fields!"); - return ; + return; } else { - let book = new Book(titleInput.value, authorInput.value,Number(pagesInput.value), isReadInput.checked); + let book = new Book( + titleValue, + authorValue, + pagesValue, + isReadInput.checked + ); myLibrary.push(book); render(); } @@ -52,27 +59,24 @@ 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.getElementById("tbody"); + tableBody.textContent = ""; //insert updated row and cells let length = myLibrary.length; for (let i = 0; i < length; i++) { - let row = table.insertRow(1); // why (1) not i+1 insert with four - let titleCell = row.insertCell(0); // the table.rows and row.cells are HTMLCollection,not a real array, but they behave like array(indexed,length) . they are DOM collections + let row = tableBody.insertRow(); // why (1) not i+1 insert with four + let titleCell = row.insertCell(0); // the table.rows and row.cells are HTMLCollection,not a real array, but they behave like array(indexed,length) . they are DOM collections let authorCell = row.insertCell(1); let pagesCell = row.insertCell(2); let wasReadCell = row.insertCell(3); - let deleteCell = row.insertCell(4); // it is like insert for - titleCell.innerHTML = myLibrary[i].title; //filling the cells why innerHTML + let deleteCell = row.insertCell(4); // it is like insert for + titleCell.innerHTML = myLibrary[i].title; //filling the cells why innerHTML authorCell.innerHTML = myLibrary[i].author; pagesCell.innerHTML = myLibrary[i].pages; //add and wait for action for read/unread button let changeBut = document.createElement("button"); - changeBut.id = i; // give the button the index of the book object + changeBut.id = i; // give the button the index of the book object changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); let readStatus = ""; @@ -88,19 +92,18 @@ function render() { render(); }); - //add delete button to every row and render again + //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 () { // + delButton.addEventListener("click", function () { + // alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); - }); } } - From 62d1b204d0ee9f9d007165dc13cc3956a547e329 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Wed, 17 Dec 2025 21:32:14 +0000 Subject: [PATCH 13/19] use textContent . change alert place to after delete process --- debugging/book-library/script.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index e5c8dd55..0b750c7f 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -70,39 +70,38 @@ function render() { let pagesCell = row.insertCell(2); let wasReadCell = row.insertCell(3); let deleteCell = row.insertCell(4); // it is like insert for - titleCell.innerHTML = myLibrary[i].title; //filling the cells why innerHTML - authorCell.innerHTML = myLibrary[i].author; - pagesCell.innerHTML = myLibrary[i].pages; + titleCell.textContent = myLibrary[i].title; //filling the cells why innerHTML + 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; // give the button the index of the book object - changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); + let changeReadBut = document.createElement("button"); + changeReadBut.className = "btn btn-success"; + wasReadCell.appendChild(changeReadBut); let readStatus = ""; if (myLibrary[i].check === true) { readStatus = "Yes"; } else { readStatus = "No"; } - changeBut.innerText = readStatus; + changeReadBut.textContent = readStatus; - changeBut.addEventListener("click", function () { + changeReadBut.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 deleteBut = document.createElement("button"); + deleteCell.appendChild(deleteBut); + deleteBut.className = "btn btn-warning"; + deleteBut.textContent = "Delete"; + deleteBut.addEventListener("click", function () { // - alert(`You've deleted title: ${myLibrary[i].title}`); + myLibrary.splice(i, 1); + alert(`You've deleted title: ${myLibrary[i].title}`); render(); }); } From d34f4fdd0c2b8653e71dce0e8d5c0ec28b73a131 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Wed, 17 Dec 2025 21:41:43 +0000 Subject: [PATCH 14/19] add deletedtitle variable to store the title --- debugging/book-library/script.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 0b750c7f..bcc93f83 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -98,10 +98,9 @@ function render() { deleteBut.className = "btn btn-warning"; deleteBut.textContent = "Delete"; deleteBut.addEventListener("click", function () { - // - + const DeletedTitle = myLibrary[i].title; myLibrary.splice(i, 1); - alert(`You've deleted title: ${myLibrary[i].title}`); + alert(`You've deleted title: ${DeletedTitle}`); render(); }); } From eb7a6bda4f4566f88c920d86cf14b9a8e49323fb Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Thu, 18 Dec 2025 13:28:54 +0000 Subject: [PATCH 15/19] add validtion for pagesvalue --- 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 bcc93f83..b86d8e7b 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -35,7 +35,7 @@ function submit() { const titleValue = titleInput.value.trim(); const authorValue = authorInput.value.trim(); const pagesValue = Number(pagesInput.value); - if (!titleValue || !authorValue || !pagesValue || pagesValue < 0) { + if (!titleValue || !authorValue || Number.isFinite(!pagesValue) || pagesValue <= 0) { alert("Please fill all fields!"); return; } else { From 9c648ef07891d464faf2fced1af834b24ca6a9a5 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Thu, 18 Dec 2025 13:33:47 +0000 Subject: [PATCH 16/19] use short hand if --- debugging/book-library/script.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index b86d8e7b..c2d53481 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -84,6 +84,8 @@ function render() { } else { readStatus = "No"; } + readStatus= myLibrary[i].check===true? "yes":"no"; + changeReadBut.textContent = readStatus; changeReadBut.addEventListener("click", function () { @@ -98,9 +100,9 @@ function render() { deleteBut.className = "btn btn-warning"; deleteBut.textContent = "Delete"; deleteBut.addEventListener("click", function () { - const DeletedTitle = myLibrary[i].title; + const deletedTitle = myLibrary[i].title; myLibrary.splice(i, 1); - alert(`You've deleted title: ${DeletedTitle}`); + alert(`You've deleted title: ${deletedTitle}`); render(); }); } From b6adcd2c1130ba7617b323489ce0bc0dde6a718d Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Thu, 18 Dec 2025 13:37:42 +0000 Subject: [PATCH 17/19] use const --- debugging/book-library/script.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index c2d53481..b0b8a749 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -64,12 +64,12 @@ function render() { //insert updated row and cells let length = myLibrary.length; for (let i = 0; i < length; i++) { - let row = tableBody.insertRow(); // why (1) not i+1 insert with four - let titleCell = row.insertCell(0); // the table.rows and row.cells are HTMLCollection,not a real array, but they behave like array(indexed,length) . they are DOM collections - let authorCell = row.insertCell(1); - let pagesCell = row.insertCell(2); - let wasReadCell = row.insertCell(3); - let deleteCell = row.insertCell(4); // it is like insert for + const row = tableBody.insertRow(); // why (1) not i+1 insert with four + const titleCell = row.insertCell(0); // the table.rows and row.cells are HTMLCollection,not a real array, but they behave like array(indexed,length) . they are DOM collections + const authorCell = row.insertCell(1); + const pagesCell = row.insertCell(2); + const wasReadCell = row.insertCell(3); + const deleteCell = row.insertCell(4); // it is like insert for titleCell.textContent = myLibrary[i].title; //filling the cells why innerHTML authorCell.textContent = myLibrary[i].author; pagesCell.textContent = myLibrary[i].pages; From 02a6b5fe547163bb2d2a6443e1dbdb0b8eedbc8d Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Thu, 18 Dec 2025 14:03:12 +0000 Subject: [PATCH 18/19] check all variable fix validtion clean the code --- debugging/book-library/script.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index b0b8a749..82c0fb3d 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -16,8 +16,8 @@ window.addEventListener("load", function (e) { //just one thing need to check function populateStorage() { if (myLibrary.length === 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true); - let book2 = new Book( + const book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true); + const book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", 127, @@ -35,11 +35,11 @@ function submit() { const titleValue = titleInput.value.trim(); const authorValue = authorInput.value.trim(); const pagesValue = Number(pagesInput.value); - if (!titleValue || !authorValue || Number.isFinite(!pagesValue) || pagesValue <= 0) { + if (!titleValue || !authorValue || !Number.isFinite(pagesValue) || pagesValue <= 0) { alert("Please fill all fields!"); return; } else { - let book = new Book( + const book = new Book( titleValue, authorValue, pagesValue, @@ -58,11 +58,10 @@ function Book(title, author, pages, check) { } function render() { - let table = document.getElementById("display"); const tableBody = document.getElementById("tbody"); tableBody.textContent = ""; //insert updated row and cells - let length = myLibrary.length; + const length = myLibrary.length; for (let i = 0; i < length; i++) { const row = tableBody.insertRow(); // why (1) not i+1 insert with four const titleCell = row.insertCell(0); // the table.rows and row.cells are HTMLCollection,not a real array, but they behave like array(indexed,length) . they are DOM collections @@ -75,15 +74,10 @@ function render() { pagesCell.textContent = myLibrary[i].pages; //add and wait for action for read/unread button - let changeReadBut = document.createElement("button"); + const changeReadBut = document.createElement("button"); changeReadBut.className = "btn btn-success"; wasReadCell.appendChild(changeReadBut); let readStatus = ""; - if (myLibrary[i].check === true) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } readStatus= myLibrary[i].check===true? "yes":"no"; changeReadBut.textContent = readStatus; From 49305b043d95904fe48969b40cfce600eb071e4d Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Thu, 18 Dec 2025 14:07:15 +0000 Subject: [PATCH 19/19] fix validtion --- 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 82c0fb3d..bebd92f5 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -35,7 +35,7 @@ function submit() { const titleValue = titleInput.value.trim(); const authorValue = authorInput.value.trim(); const pagesValue = Number(pagesInput.value); - if (!titleValue || !authorValue || !Number.isFinite(pagesValue) || pagesValue <= 0) { + if (!titleValue || !authorValue || !Number.isInteger(pagesValue) || pagesValue <= 0) { alert("Please fill all fields!"); return; } else { @@ -89,7 +89,7 @@ function render() { //add delete button to every row and render again - let deleteBut = document.createElement("button"); + const deleteBut = document.createElement("button"); deleteCell.appendChild(deleteBut); deleteBut.className = "btn btn-warning"; deleteBut.textContent = "Delete";