-
-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
West Midlands | Gabriel Deng | Module-Data-Groups | Week 2 - Book reading #131
base: main
Are you sure you want to change the base?
Changes from 4 commits
06f3a34
10feedb
f81b156
a3e8186
fb8b22f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
myLibrary.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 | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The assignment of the id in this case is not important because there is no where in the code where the button is being used in reference to the id. So i have deleted it to make the code more readable and as precise as possible. |
||
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(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
||
Comment on lines
+20
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure what you meant. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I misinterpreted how the scope works. I didn't take into account where the console logs where being called. The console inside the function will refer to the local variable x and vice-versa. |
||
## 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you try running the code to see what it produces? The second statement does not explain why the first output is 10. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, the output is indeed 10 and undefined. I try making some research and it turns out that it is because the function has no return value although y has been declared and assigned a value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I ran the script, the script threw an exception (because |
||
|
||
## 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spot on. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks |
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you think of a more efficient way to remove all rows (except the heading) in the table?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the most efficient way is to set the innerHTML of the table to emtpy and then appendChild header. In this case the first row.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have also implemented the checking of the input of spaces using the input.value.trim() and check if it is empty. If so, an alert will pop out. Similarly, the validation of the page input field is also implemented.