Skip to content
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
30 changes: 15 additions & 15 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<!DOCTYPE html>
<html>
<html lang="en-GB">
<head>
<title> </title>
<title>My VirtualLibrary</title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<meta name="description" content="A simple book library application" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
Expand All @@ -23,23 +24,23 @@ <h1>Library</h1>
<p>Add books to your virtual library</p>
</div>

<button data-toggle="collapse" data-target="#demo" class="btn btn-info">
<button type="button" data-toggle="collapse" data-target="#demo" class="btn btn-primary">
Add new book
</button>

<div id="demo" class="collapse">
<div class="form-group">
<form class="form-group">
<label for="title">Title:</label>
<input
type="title"
type="text"
class="form-control"
id="title"
name="title"
required
/>
<label for="author">Author: </label>
<input
type="author"
type="text"
class="form-control"
id="author"
name="author"
Expand All @@ -51,23 +52,22 @@ <h1>Library</h1>
class="form-control"
id="pages"
name="pages"
min="1"
required
/>
<label class="form-check-label">
<label class="form-check-label" for="check">
<input
type="checkbox"
class="form-check-input"
id="check"
value=""
/>Read
</label>
<input
type="submit"
value="Submit"
<button
id="add-book-btn"
type="button"
class="btn btn-primary"
onclick="submit();"
/>
</div>
> Add Book </button>
</form>
</div>

<table class="table" id="display">
Expand All @@ -77,7 +77,7 @@ <h1>Library</h1>
<th>Author</th>
<th>Number of Pages</th>
<th>Read</th>
<th></th>
<th>Delete</th>
</tr>
</thead>
<tbody>
Expand Down
11 changes: 6 additions & 5 deletions debugging/book-library/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ My website should be able to:

## Bugs to be fixed

1. Website loads but doesn't show any books
2. Error in console when you try to add a book
3. It uses the title name as the author name
4. Delete button is broken
5. When I add a book that I say I've read - it saves the wrong answer
1. Website loads but doesn't show any books - fixed
2. Error in console when you try to add a book - fixed
3. It uses the title name as the author name - fixed
4. Delete button is broken - fixed
5. When I add a book that I say I've read - it saves the wrong answer - fixed

I think there are other some other small bugs in my code...but I'm lazy so I can't fix them all.
- unsure if ive caught all the bugs but have made changes I thought were pertinent and ensure full lighthouse score

I wish somebody would help me!
67 changes: 33 additions & 34 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ let myLibrary = [];

window.addEventListener("load", function (e) {
populateStorage();
render();
const form = document.getElementById("bookForm");
form?.addEventListener("submit", e => {
e.preventDefault();
});
const addBookBtn = document.getElementById("addBookBtn");
addBookBtn?.addEventListener("click", addBook);
});

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",
Expand All @@ -20,25 +25,24 @@ function populateStorage() {
}
}

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() {
if (
title.value == null ||
title.value == "" ||
pages.value == null ||
pages.value == ""
) {
function addBook() {
const title = document.getElementById("title");
const author = document.getElementById("author");
const pages = document.getElementById("pages");
const check = document.getElementById("check");
if (!title.value || !author.value || !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 book = new Book(title.value, author.value, pages.value, check.checked);
myLibrary.push(book);
title.value = "";
author.value = "";
pages.value = "";
check.checked = false;
alert (`You've added ${book.title} to your library.`);
render();
}
}
Expand All @@ -54,7 +58,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 @@ -66,22 +70,17 @@ function render() {
let pagesCell = row.insertCell(2);
let wasReadCell = row.insertCell(3);
let deleteCell = row.insertCell(4);
titleCell.innerHTML = myLibrary[i].title;
authorCell.innerHTML = myLibrary[i].author;
pagesCell.innerHTML = myLibrary[i].pages;
titleCell.innerText = myLibrary[i].title;
authorCell.innerText = myLibrary[i].author;
pagesCell.innerText = myLibrary[i].pages;

//add and wait for action for read/unread button
let changeBut = document.createElement("button");
changeBut.id = i;
changeBut.className = "btn btn-success";
changeBut.type = "button";
wasReadCell.appendChild(changeBut);
let readStatus = "";
if (myLibrary[i].check == false) {
readStatus = "Yes";
} else {
readStatus = "No";
}
let readStatus = myLibrary[i].check ? "Yes" : "No";
changeBut.innerText = readStatus;
changeBut.className = 'btn '+ (myLibrary[i].check ? 'btn-success' : 'btn-danger');

changeBut.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
Expand All @@ -90,12 +89,12 @@ 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}`);
delButton.type = "button";
deleteCell.appendChild(delButton);
delButton.className = "btn btn-danger";
delButton.innerText = "Delete";
delButton.addEventListener("click", function () {
alert(`You've deleted ${myLibrary[i].title} from your library.`);
myLibrary.splice(i, 1);
render();
});
Expand Down
19 changes: 15 additions & 4 deletions debugging/book-library/style.css
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
.collapse.show{
display: flex;
}

.form-group {
width: 400px;
height: 300px;
align-self: left;
align-self: flex-start;
padding-left: 20px;
}

.btn {
display: block;
font-weight: 600;
color: black;
}

.form-check-label {
padding-left: 20px;
margin: 5px 0px 5px 0px;
padding-top: 10px;
margin: 5px ;
}

button.btn-info {
button[data-toggle="collapse"] {
margin: 20px;
}

#add-book-btn {
display: block;
margin-top: 10px;
}
8 changes: 8 additions & 0 deletions debugging/code-reading/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Take a look at the following code:

Explain why line 5 and line 8 output different numbers.

Due to x holding different values in global scope and functional scope

## Question 2

Take a look at the following code:
Expand All @@ -35,6 +37,9 @@ console.log(y);

What will be the output of this code. Explain your answer in 50 words or less.

The first console.log will log 10 to the console, while the second will log undefined and throw a reference error due to y only being available in the function


## Question 3

Take a look at the following code:
Expand Down Expand Up @@ -62,3 +67,6 @@ console.log(y);
```

What will be the output of this code. Explain your answer in 50 words or less.

First console.log outputs 9 as x is passed and the f1 call doesn't alter its value outside the fn.
Second console.log prints the object {x:10} as f2 increments y.x by 1
Loading