From 3d8b1b7ae83583e97e72e8fa03e2b9e57da4b315 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 16 Apr 2026 12:08:33 +0100 Subject: [PATCH 1/5] librery should work as expected now --- debugging/book-library/script.js | 89 ++++++++++++++++++++------------ debugging/code-reading/readme.md | 9 ++++ 2 files changed, 65 insertions(+), 33 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..36c4945a 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -5,41 +5,49 @@ window.addEventListener("load", function (e) { render(); }); + + + 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", - "Ernest Hemingway", - "127", - true - ); - myLibrary.push(book1); - myLibrary.push(book2); - render(); + "Ernest Hemingway", "5", true); + myLibrary.push(book1, 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 title = document.getElementById("title").value; + const author = document.getElementById("author").value; + const pages = document.getElementById("pages").value; + const check = document.getElementById("check").checked; if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" - ) { + + title == "" || + pages == ""|| + author == "" + ) + + { alert("Please fill all fields!"); - return false; + return true; } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); + let book = new Book(title, author, pages, check); + myLibrary.push(book); render(); + //clear the input values + + } } @@ -54,12 +62,17 @@ 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 - let length = myLibrary.length; - for (let i = 0; i < length; i++) { + let 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); @@ -69,6 +82,11 @@ function render() { titleCell.innerHTML = myLibrary[i].title; authorCell.innerHTML = myLibrary[i].author; pagesCell.innerHTML = myLibrary[i].pages; + + //deleteCell.innerHTML = delBut[i] + + + //add and wait for action for read/unread button let changeBut = document.createElement("button"); @@ -76,7 +94,7 @@ function render() { changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == false) { + if (myLibrary[i].check == true) { readStatus = "Yes"; } else { readStatus = "No"; @@ -84,20 +102,25 @@ function render() { changeBut.innerText = readStatus; changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; - render(); - }); + if( myLibrary[i].check == true){ + myLibrary[i].check = false; + } + else { + myLibrary[i].check = true; + } + + render(); + }); //add delete button to every row and render again - let delButton = document.createElement("button"); - delBut.id = i + 5; + let delBut = document.createElement("button"); + delBut.id = i ; deleteCell.appendChild(delBut); delBut.className = "btn btn-warning"; delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); - }); + })}; } -} diff --git a/debugging/code-reading/readme.md b/debugging/code-reading/readme.md index 4090c14c..4ccd7786 100644 --- a/debugging/code-reading/readme.md +++ b/debugging/code-reading/readme.md @@ -16,6 +16,8 @@ Take a look at the following code: ``` Explain why line 5 and line 8 output different numbers. +line 5 logs the "let x" from the local scope of the function +where as line 8 uses the "let x" from line one in the global scope ## Question 2 @@ -34,6 +36,13 @@ console.log(y); ``` What will be the output of this code. Explain your answer in 50 words or less. +you will get +10 +undefined +undefined +the x is in the global scope so accsesable to f1 +f1 has no return value to log +and the y is only in the local scope of the function ## Question 3 From b0f2a10363513a4e00d79bbe898843b96ff381dd Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 16 Apr 2026 12:14:51 +0100 Subject: [PATCH 2/5] file fixed --- debugging/code-reading/readme.md | 9 --------- 1 file changed, 9 deletions(-) diff --git a/debugging/code-reading/readme.md b/debugging/code-reading/readme.md index 4ccd7786..4090c14c 100644 --- a/debugging/code-reading/readme.md +++ b/debugging/code-reading/readme.md @@ -16,8 +16,6 @@ Take a look at the following code: ``` Explain why line 5 and line 8 output different numbers. -line 5 logs the "let x" from the local scope of the function -where as line 8 uses the "let x" from line one in the global scope ## Question 2 @@ -36,13 +34,6 @@ console.log(y); ``` What will be the output of this code. Explain your answer in 50 words or less. -you will get -10 -undefined -undefined -the x is in the global scope so accsesable to f1 -f1 has no return value to log -and the y is only in the local scope of the function ## Question 3 From c2e4d17fcc53a08eff8e693a5fa0be7c931db46d Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 17 Apr 2026 12:56:55 +0100 Subject: [PATCH 3/5] made a start on the refactor stuck on how to clear the table at top of render function --- debugging/book-library/index.html | 2 +- debugging/book-library/script.js | 132 +++++++++++++----------------- 2 files changed, 56 insertions(+), 78 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..8b315f5e 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -91,6 +91,6 @@

Library

- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 36c4945a..1e44671e 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,126 +1,104 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", function () { populateStorage(); - render(); }); - - - 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", "5", true); + "Ernest Hemingway", + "500", + true + ); myLibrary.push(book1, book2); - - render(); + render(); } } - - //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 title = document.getElementById("title").value; - const author = document.getElementById("author").value; - const pages = document.getElementById("pages").value; - const check = document.getElementById("check").checked; - if ( - - title == "" || - pages == ""|| - author == "" - ) - - { + const titleInput = document.getElementById("title").value; + const authorInput = document.getElementById("author").value; + const pagesInput = document.getElementById("pages").value; + const checkInput = document.getElementById("check").checked; + if (titleInput == "" || pagesInput == "" || authorInput == "") { alert("Please fill all fields!"); return true; } else { - let book = new Book(title, author, pages, check); + let book = new Book(titleInput, authorInput, pagesInput, checkInput); myLibrary.push(book); render(); - //clear the input values - - + //clear the input values } } -function Book(title, author, pages, check) { - this.title = title; - this.author = author; - this.pages = pages; - this.check = check; +function Book(titleInput, authorInput, pagesInput, checkInput) { + this.title = titleInput; + this.author = authorInput; + this.pages = pagesInput; + this.check = checkInput; } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; + const table = document.getElementById("display"); + const 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--) { + table.deleteRow(n); + } - //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); + const row = table.insertRow(1); + 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.innerHTML = myLibrary[i].title; authorCell.innerHTML = myLibrary[i].author; pagesCell.innerHTML = myLibrary[i].pages; - - //deleteCell.innerHTML = delBut[i] - - + //deleteCell.innerHTML = button[i] //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); + const changeButton = document.createElement("button"); + changeButton.id = i; + changeButton.className = "btn btn-success"; + wasReadCell.appendChild(changeButton); let readStatus = ""; - if (myLibrary[i].check == true) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; - changeBut.addEventListener("click", function () { + myLibrary[i].check == true + ? (readStatus = "Yes") + : (readStatus = "No"); + + changeButton.innerText = readStatus; - if( myLibrary[i].check == true){ - myLibrary[i].check = false; - } - else { - myLibrary[i].check = true; - } + changeButton.addEventListener("click", function () { + myLibrary[i].check == true + ? (myLibrary[i].check = false) + : (myLibrary[i].check = true); render(); - }); + }); //add delete button to every row and render again - let delBut = document.createElement("button"); - delBut.id = i ; - deleteCell.appendChild(delBut); - delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("click", function () { + const button = document.createElement("button"); + button.id = i; + deleteCell.appendChild(button); + button.className = "btn btn-warning"; + button.innerHTML = "Delete"; + button.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); - })}; + }); } +} From cd3f7dd69537487206d409847f5dbc30ac6c6b8d Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 17 Apr 2026 13:01:46 +0100 Subject: [PATCH 4/5] i think i got it --- 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 1e44671e..79cb052c 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -49,11 +49,11 @@ function render() { const table = document.getElementById("display"); const rowsNumber = table.rows.length; //delete old table - - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); +while (table.rows.length > 1) { + table.deleteRow(1); } + //insert updated row and cells const length = myLibrary.length; for (let i = 0; i <= length; i++) { From 6757af640547b065d53a8a262d6631050bcd725e Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 17 Apr 2026 17:04:19 +0100 Subject: [PATCH 5/5] 12 of the 13 issuse adressed haveing some trouble with the delete order --- debugging/book-library/index.html | 30 ++++++++++---------- debugging/book-library/script.js | 46 ++++++++++++++----------------- 2 files changed, 37 insertions(+), 39 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 8b315f5e..707a2510 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,20 +1,18 @@ - + - + Library Books + > - + > + @@ -31,42 +29,46 @@

Library

+ > + > + > + >
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 79cb052c..9d295062 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,4 +1,4 @@ -let myLibrary = []; +const myLibrary = []; window.addEventListener("load", function () { populateStorage(); @@ -23,18 +23,22 @@ function populateStorage() { //via Book function and start render function function submit() { - const titleInput = document.getElementById("title").value; - const authorInput = document.getElementById("author").value; + const titleInput = document.getElementById("title").value.trimStart(); + const authorInput = document.getElementById("author").value.trimStart(); const pagesInput = document.getElementById("pages").value; const checkInput = document.getElementById("check").checked; - if (titleInput == "" || pagesInput == "" || authorInput == "") { + if ( + titleInput == "" || + pagesInput == "" || + authorInput == "" || + pagesInput < 1 || + pagesInput > 16000 + ) { alert("Please fill all fields!"); - return true; } else { let book = new Book(titleInput, authorInput, pagesInput, checkInput); myLibrary.push(book); render(); - //clear the input values } } @@ -49,56 +53,48 @@ function render() { const table = document.getElementById("display"); const rowsNumber = table.rows.length; //delete old table -while (table.rows.length > 1) { - table.deleteRow(1); + while (table.rows.length > 1) { + table.deleteRow(1); } - //insert updated row and cells const length = myLibrary.length; - for (let i = 0; i <= length; i++) { + for (let i = 0; i < length; i++) { const row = table.insertRow(1); 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.innerHTML = myLibrary[i].title; - authorCell.innerHTML = myLibrary[i].author; - pagesCell.innerHTML = myLibrary[i].pages; - - //deleteCell.innerHTML = button[i] + titleCell.textContent = myLibrary[i].title; + authorCell.textContent = myLibrary[i].author; + pagesCell.textContent = myLibrary[i].pages; //add and wait for action for read/unread button const changeButton = document.createElement("button"); - changeButton.id = i; + changeButton.className = "btn btn-success"; wasReadCell.appendChild(changeButton); let readStatus = ""; - myLibrary[i].check == true - ? (readStatus = "Yes") - : (readStatus = "No"); + myLibrary[i].check ? (readStatus = "Yes") : (readStatus = "No"); changeButton.innerText = readStatus; changeButton.addEventListener("click", function () { - myLibrary[i].check == true - ? (myLibrary[i].check = false) - : (myLibrary[i].check = true); + myLibrary[i].check = !myLibrary[i].check; render(); }); //add delete button to every row and render again const button = document.createElement("button"); - button.id = i; deleteCell.appendChild(button); button.className = "btn btn-warning"; - button.innerHTML = "Delete"; + button.textContent = "Delete"; button.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); + alert(`You've deleted title: ${myLibrary[i].title}`); }); } }