From f68ec01fc1ee63992dfd360249167268fb6eedd4 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Tue, 25 Mar 2025 16:19:03 +0000 Subject: [PATCH 1/2] completed sprint 2 debugging --- debugging/book-library/script.js | 42 +++++++++++++++++++------------- debugging/code-reading/readme.md | 3 +++ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1..36760d8 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -6,8 +6,8 @@ window.addEventListener("load", function (e) { }); function populateStorage() { - if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); + if (myLibrary.length === 0) { + let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true); let book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", @@ -29,17 +29,23 @@ const check = document.getElementById("check"); //via Book function and start render function function submit() { if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" + title.value === null || + title.value === "" || + author.value === null || + 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 newBook = new Book(title.value, author.value, pages.value, check.checked); + myLibrary.push(newBook); render(); + title.value = ""; + author.value = ""; + pages.value = null; + check.checked = false; } } @@ -54,7 +60,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 @@ -72,14 +78,16 @@ function render() { //add and wait for action for read/unread button let changeBut = document.createElement("button"); - changeBut.id = i; - changeBut.className = "btn btn-success"; + //changeBut.id = i; + wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == false) { + if (myLibrary[i].check !== false) { readStatus = "Yes"; + changeBut.className = "btn btn-success"; } else { readStatus = "No"; + changeBut.className = "btn btn-danger"; } changeBut.innerText = readStatus; @@ -90,11 +98,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("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 4090c14..19c6e9d 100644 --- a/debugging/code-reading/readme.md +++ b/debugging/code-reading/readme.md @@ -16,6 +16,7 @@ Take a look at the following code: ``` Explain why line 5 and line 8 output different numbers. +Answer: line 5 output local variable x (2) from the function without returning it and line 8 output global variable x (1) from the main program. ## Question 2 @@ -34,6 +35,7 @@ console.log(y); ``` What will be the output of this code. Explain your answer in 50 words or less. +Answer: output will be 10 and undefined because the first log will display global x (10) and the 2nd log will be undefined as y is declared in the function locally but isn't returned globally. ## Question 3 @@ -62,3 +64,4 @@ console.log(y); ``` What will be the output of this code. Explain your answer in 50 words or less. +Answer: first part returns 9 as x because f1 creates a copy of x and handles and stores the result but x stays the same. 2nd part returns a modified object y as { x: 10} because f2 modifies the object. \ No newline at end of file From 410560780b4d957827e50bed9aaec7de0f9203d0 Mon Sep 17 00:00:00 2001 From: Fradoka Date: Sat, 12 Apr 2025 08:41:15 +0100 Subject: [PATCH 2/2] edited script.js --- debugging/book-library/script.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 36760d8..b375bdf 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -29,12 +29,9 @@ const check = document.getElementById("check"); //via Book function and start render function function submit() { if ( - title.value === null || - title.value === "" || - author.value === null || - author.value === "" || - pages.value === null || - pages.value === "" + !(title.value.trim()) || + !(author.value.trim()) || + !(pages.value.trim()) ) { alert("Please fill all fields!"); return false; @@ -103,9 +100,13 @@ function render() { delButton.className = "btn btn-warning"; delButton.innerHTML = "Delete"; delButton.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); - myLibrary.splice(i, 1); - render(); - }); + const confirmDelete = window.confirm("Are you sure you want to delete this book?"); + if (confirmDelete) { + myLibrary.splice(i, 1); + render(); + alert(`You've deleted title: ${myLibrary[i].title}`); + } else { + console.log("Deletion canceled"); + }}); } }