Skip to content

London_ITP_Jan_2025 | FRANKLIN KAMELA | Module_Data_Flows | Book_Library | Sprint_2 #171

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -29,17 +29,20 @@ 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.trim()) ||
!(author.value.trim()) ||
!(pages.value.trim())
) {
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;
}
}

Expand All @@ -54,7 +57,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
Expand All @@ -72,14 +75,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;

Expand All @@ -90,14 +95,18 @@ 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 () {
alert(`You've deleted title: ${myLibrary[i].title}`);
myLibrary.splice(i, 1);
render();
});
//delButton.id = i + 5;
deleteCell.appendChild(delButton);
delButton.className = "btn btn-warning";
delButton.innerHTML = "Delete";
delButton.addEventListener("click", function () {
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");
}});
}
}
3 changes: 3 additions & 0 deletions debugging/code-reading/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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.