Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fee43ee
Refactor HTML structure, enhance form validation, and improve JavaScr…
i786m Nov 25, 2025
2f35ce1
Merge branch 'CodeYourFuture:main' into coursework/booklibrary
i786m Dec 6, 2025
2648f54
fix meta tag errors in html
i786m Dec 8, 2025
bcf8c09
update script tag to use module type
i786m Dec 8, 2025
f95fbc6
fix ID for add book button in event listener
i786m Dec 8, 2025
9304166
use querySelector to target form tag
i786m Dec 8, 2025
df6e09d
convert pages value to a number when adding a book
i786m Dec 8, 2025
778d81d
change variable names for input elements to reflect what they store
i786m Dec 8, 2025
aea2a43
fix typo in book1
i786m Dec 8, 2025
58798b5
normalize, sanitize and validate data on input
i786m Dec 8, 2025
2a58ad6
refactor to delete rows in one operation
i786m Dec 8, 2025
8ac46e3
fix delete message timing issue
i786m Dec 8, 2025
5691196
use textContent instead of innerText for rendering book details
i786m Dec 8, 2025
f6a4060
improve button naming and clarity
i786m Dec 8, 2025
d9acf45
fix page numbers in populateStorage function to use numbers
i786m Dec 8, 2025
c6bf51d
refactor addBook and render functions
i786m Dec 8, 2025
e6143cb
use textContent instead of innerText for buttons
i786m Dec 8, 2025
ea49aa2
removed trailing slash on void elements
i786m Dec 8, 2025
f2a5fcf
remove empty table row in book display
i786m Dec 8, 2025
620b93c
refactor render to protect against null tbody
i786m Dec 8, 2025
0b76c33
format button label for better readability and update button styles
i786m Dec 8, 2025
d349fa1
update button colors for accessibility
i786m Dec 8, 2025
cbcd993
tidy up
i786m Dec 8, 2025
7f03371
add button color for improved visibility
i786m Dec 8, 2025
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");
Comment thread
i786m marked this conversation as resolved.
Outdated
form?.addEventListener("submit", e => {
e.preventDefault();
});
const addBookBtn = document.getElementById("addBookBtn");
Comment thread
i786m marked this conversation as resolved.
Outdated
addBookBtn?.addEventListener("click", addBook);
});

function populateStorage() {
if (myLibrary.length == 0) {
if (myLibrary.length === 0) {
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
Comment thread
i786m marked this conversation as resolved.
Outdated
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.`);
Comment thread
i786m marked this conversation as resolved.
Outdated
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');
Comment thread
i786m marked this conversation as resolved.
Outdated

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 ;
Comment thread
i786m marked this conversation as resolved.
}

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
Comment thread
i786m marked this conversation as resolved.


## 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
Comment thread
i786m marked this conversation as resolved.
Comment on lines +71 to +72
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Look up the relevant technical terms, pass by value and pass by reference.

Loading