Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
026ab7c
Fix: Corrected variable name from 'library' to 'myLibrary' in submit …
alexandru-pocovnicu Apr 6, 2026
ba1e8fc
Fix: Corrected syntax error in the render function's for loop
alexandru-pocovnicu Apr 6, 2026
9152461
Fix: Corrected button creation and event listener for delete function…
alexandru-pocovnicu Apr 6, 2026
2be9382
Fix: Corrected validation check for author input in submit function
alexandru-pocovnicu Apr 6, 2026
2ce8154
Fix: Corrected logic for book read status in submit function
alexandru-pocovnicu Apr 6, 2026
e160341
Fix: Corrected event listener type for delete button in render function
alexandru-pocovnicu Apr 6, 2026
62672c3
Fix: Corrected author value assignment in book creation within submit…
alexandru-pocovnicu Apr 6, 2026
31236b1
Fix: Corrected book creation to use checked state for read status in …
alexandru-pocovnicu Apr 6, 2026
88c944d
Fix: Corrected read status display logic in render function
alexandru-pocovnicu Apr 6, 2026
e9a419f
Fix: Added validation for pages input in submit function
alexandru-pocovnicu Apr 6, 2026
9e5e015
Fix: Added language attribute to html tag and updated title in index.…
alexandru-pocovnicu Apr 6, 2026
6eef9d9
Fix: Standardized HTML syntax and formatting in index.html
alexandru-pocovnicu Apr 6, 2026
cc880b1
delete trailing slash
alexandru-pocovnicu Apr 6, 2026
0a0dcf3
Fix: Corrected input types for title and author fields in index.html
alexandru-pocovnicu Apr 6, 2026
8e1e374
Fix: Corrected meta tag and input element syntax in index.html
alexandru-pocovnicu Apr 9, 2026
02d14fc
Fix: Removed unnecessary align-self property from .form-group in styl…
alexandru-pocovnicu Apr 9, 2026
ca4167d
Fix: Corrected self-closing tags and standardized HTML syntax in inde…
alexandru-pocovnicu Apr 9, 2026
866e223
Fix: Remove unnecessary render call on window load and clean up white…
alexandru-pocovnicu Apr 17, 2026
b4fe64c
set time delay for alert message
alexandru-pocovnicu Apr 17, 2026
3c8c704
Rename button variables for clarity
alexandru-pocovnicu Apr 17, 2026
e9452d6
Fix: Remove unnecessary IDs from buttons in render function
alexandru-pocovnicu Apr 17, 2026
e2af0f5
Fix: Replace innerHTML with textContent for safer DOM manipulation in…
alexandru-pocovnicu Apr 17, 2026
f72d535
Fix: Update validation checks in submit function for better input han…
alexandru-pocovnicu Apr 17, 2026
07a2811
Fix: Refactor render function to use tbody for better DOM manipulatio…
alexandru-pocovnicu Apr 17, 2026
f24b3b1
Fix: Refactor submit function to use trimmed values for better input …
alexandru-pocovnicu Apr 17, 2026
c0410cc
Fix: Update pages value handling in submit function for improved inpu…
alexandru-pocovnicu Apr 17, 2026
71af8ec
Fix: Rename input variable references for consistency and clarity in …
alexandru-pocovnicu Apr 17, 2026
fff5df5
Fix: Correct pages value type in Book instantiation and update valida…
alexandru-pocovnicu Apr 17, 2026
0e1d107
Fix: Change myLibrary to const and move input variable declarations t…
alexandru-pocovnicu Apr 17, 2026
6f8c193
change if statement with ternary operator
alexandru-pocovnicu Apr 17, 2026
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
19 changes: 8 additions & 11 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<!DOCTYPE html>
<html>
<!doctype html>
<html lang="en">
<head>
<title> </title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Book Library</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<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 @@ -31,15 +28,15 @@ <h1>Library</h1>
<div 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 @@ -65,7 +62,7 @@ <h1>Library</h1>
type="submit"
value="Submit"
class="btn btn-primary"
onclick="submit();"
onclick="submit()"
/>
</div>
</div>
Expand Down
91 changes: 47 additions & 44 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
let myLibrary = [];

const myLibrary = [];
const titleInput = document.getElementById("title");
const authorInput = document.getElementById("author");
const pagesInput = document.getElementById("pages");
const readCheckbox = document.getElementById("check");
window.addEventListener("load", function (e) {
populateStorage();
render();
});

function populateStorage() {
if (myLibrary.length == 0) {
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
let book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true);
let book2 = new Book(
"The Old Man and the Sea",
"Ernest Hemingway",
"127",
127,
true
);
myLibrary.push(book1);
Expand All @@ -20,25 +22,31 @@ 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() {
const titleValue = titleInput.value.trim();
const authorValue = authorInput.value.trim();
const pagesValue = pagesInput.value.trim();
const pagesCount = Number(pagesValue);

if (
title.value == null ||
title.value == "" ||
pages.value == null ||
pages.value == ""
titleValue == "" ||
authorValue == "" ||
pagesValue == "" ||
pagesCount < 0 ||
!Number.isInteger(pagesCount)
) {
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(
titleValue,
authorValue,
pagesCount,
readCheckbox.checked
);
myLibrary.push(book);
render();
}
}
Expand All @@ -51,53 +59,48 @@ function Book(title, author, pages, check) {
}

function render() {
let table = document.getElementById("display");
let rowsNumber = table.rows.length;
//delete old table
for (let n = rowsNumber - 1; n > 0; n-- {
table.deleteRow(n);
}
let tableBody = document.querySelector("tbody");
tableBody.innerHTML = "";

//insert updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
let row = table.insertRow(1);
let row = tableBody.insertRow();
let titleCell = row.insertCell(0);
let authorCell = row.insertCell(1);
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.textContent = myLibrary[i].title;
authorCell.textContent = myLibrary[i].author;
pagesCell.textContent = 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";
wasReadCell.appendChild(changeBut);
let changeButton = document.createElement("button");
changeButton.className = "btn btn-success";
wasReadCell.appendChild(changeButton);
let readStatus = "";
if (myLibrary[i].check == false) {
readStatus = "Yes";
} else {
readStatus = "No";
}
changeBut.innerText = readStatus;

changeBut.addEventListener("click", function () {
myLibrary[i].check == false ? (readStatus = "No") : (readStatus = "Yes");
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.

expr1 ? expr2 : expr3 is an expression that evalutes to the value of either expr2 or expr3 depending
on the truthy value of expr1.

Normal use of ? : is

variable = expr1 ? expr2 : expr3;

instead of

expr1 ? (variable = expr2) : (variable = expr3);

Note: .check is a boolean value. You can just use its value directly without comparing it to true or false.

changeButton.textContent = readStatus;
Comment on lines 82 to +85
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.

This could be a good opportunity to practice using the ? : conditional operator. Can you rewrite the code on lines 84–90 as a single statement?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done


changeButton.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
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}`);
let deleteButton = document.createElement("button");
deleteCell.appendChild(deleteButton);
deleteButton.className = "btn btn-warning";
deleteButton.textContent = "Delete";
deleteButton.addEventListener("click", function () {
const deletedTitle = myLibrary[i].title;
myLibrary.splice(i, 1);
render();
setTimeout(function () {
alert(`You've deleted title: ${deletedTitle}`);
}, 500);
});
}
}
1 change: 0 additions & 1 deletion debugging/book-library/style.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.form-group {
width: 400px;
height: 300px;
align-self: left;
padding-left: 20px;
}

Expand Down
Loading