From 06f3a344c7b40d28d7801fc8d3097f6ae8d871eb Mon Sep 17 00:00:00 2001 From: gai93003 Date: Thu, 9 Jan 2025 01:11:35 +0000 Subject: [PATCH 1/4] There was a missing parenthesis, i also change the mis-spelling of the variable name myLibrary. Also change the title.value to author.value --- 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 75ce6c1..51d4d86 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -37,8 +37,8 @@ function submit() { 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); + myLibrarylibrary.push(book); render(); } } @@ -54,7 +54,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 From 10feedbd4a061c4b4356037bcf0dd7beed64bde8 Mon Sep 17 00:00:00 2001 From: gai93003 Date: Thu, 9 Jan 2025 01:21:39 +0000 Subject: [PATCH 2/4] Made some changes on the code --- 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 51d4d86..27fc26c 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -76,7 +76,7 @@ function render() { changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == false) { + if (myLibrary[i].check) { readStatus = "Yes"; } else { readStatus = "No"; @@ -90,11 +90,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("clicks", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From f81b15683ea506fa54800e91f83afcb52ba59381 Mon Sep 17 00:00:00 2001 From: gai93003 Date: Thu, 9 Jan 2025 01:27:09 +0000 Subject: [PATCH 3/4] some more changes --- 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 27fc26c..cad1b0b 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -38,7 +38,7 @@ function submit() { return false; } else { let book = new Book(title.value, author.value, pages.value, check.checked); - myLibrarylibrary.push(book); + myLibrary.push(book); render(); } } @@ -94,7 +94,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 a3e8186203f7a408b0df20659580db4d2b611cfa Mon Sep 17 00:00:00 2001 From: gai93003 Date: Thu, 9 Jan 2025 23:17:49 +0000 Subject: [PATCH 4/4] I attempted the challenges on this file. --- debugging/code-reading/readme.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/debugging/code-reading/readme.md b/debugging/code-reading/readme.md index 4090c14..00a4e94 100644 --- a/debugging/code-reading/readme.md +++ b/debugging/code-reading/readme.md @@ -17,6 +17,9 @@ Take a look at the following code: Explain why line 5 and line 8 output different numbers. +Ans: The variable at line 5 is a globally declared as such it will return 1, while the variable at line 8 is a scope variable since is declared with the scope of the function. + Therefore, when the function is called it will console the x variable declared in its scope. + ## Question 2 Take a look at the following code: @@ -35,6 +38,8 @@ console.log(y); What will be the output of this code. Explain your answer in 50 words or less. +Ans: The output will be 10 and undefined. This is because the y variable is declared within the scope of the function and therefore cannot be globally accessed. + ## Question 3 Take a look at the following code: @@ -62,3 +67,8 @@ console.log(y); ``` What will be the output of this code. Explain your answer in 50 words or less. + +Ans: The output will be 9 and {x: 10}; This is due to the difference by which each of the +variables are passed. The variable x in f1 is passed by value, so the function will not modify the actual value of the variable. +While in f2, the variable y is passed by reference, there manipulating the code in the function will result into modification of the value +of the variable y.