Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
67 changes: 17 additions & 50 deletions debugging/book-library/index.html
Comment thread
cjyuan marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html>
<head>
<title> </title>
Expand Down Expand Up @@ -28,46 +28,21 @@ <h1>Library</h1>
</button>

<div id="demo" class="collapse">
<div class="form-group">
<label for="title">Title:</label>
<input
type="title"
class="form-control"
id="title"
name="title"
required
/>
<label for="author">Author: </label>
<input
type="author"
class="form-control"
id="author"
name="author"
required
/>
<label for="pages">Pages:</label>
<input
type="number"
class="form-control"
id="pages"
name="pages"
required
/>
<label class="form-check-label">
<input
type="checkbox"
class="form-check-input"
id="check"
value=""
/>Read
</label>
<input
type="submit"
value="Submit"
class="btn btn-primary"
onclick="submit();"
/>
</div>
<form id="bookForm">
<div class="form-group">
<label for="title">Title:</label>
<input type="text" class="form-control" id="title" required />
<label for="author">Author: </label>
<input type="text" class="form-control" id="author" required />
<label for="pages">Pages:</label>
<input type="number" class="form-control" id="pages" required />
<label class="form-check-label">
<input type="checkbox" class="form-check-input" id="check" />Read
</label>

<button type="submit" class="btn btn-primary">Enter</button>
</div>
</form>
</div>

<table class="table" id="display">
Expand All @@ -80,15 +55,7 @@ <h1>Library</h1>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
<tbody></tbody>
</table>

<script src="script.js"></script>
Expand Down
152 changes: 94 additions & 58 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,58 @@
let myLibrary = [];
let myLibrary = JSON.parse(localStorage.getItem("myLibrary")) || [];
Comment thread
cjyuan marked this conversation as resolved.
Outdated

window.addEventListener("load", function (e) {
populateStorage();
render();
});
function saveLibrary() {
localStorage.setItem("myLibrary", JSON.stringify(myLibrary));
}

document.getElementById("bookForm").addEventListener("submit", function (e) {
e.preventDefault();

const title = document.getElementById("title").value;
const author = document.getElementById("author").value;
const pages = document.getElementById("pages").value;
const read = document.getElementById("check").checked;

function populateStorage() {
if (myLibrary.length == 0) {
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
let book2 = new Book(
"The Old Man and the Sea",
"Ernest Hemingway",
"127",
true
);
myLibrary.push(book1);
myLibrary.push(book2);
render();
if (!title || !author || !pages) {
alert("Please fill all fields!");
return;
}
Comment thread
cjyuan marked this conversation as resolved.
Outdated

populateStorage(title, author, pages, read);

render();

this.reset();
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 seems quite advanced. Are you familiar with the meaning of this in JS?

In Piscine, you may be asked something like this:

  • If you were not allowed to use this, how would you rewrite this.reset()?

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.

If I couldn’t use this.reset(), I would manually reset each form field by setting their values back to empty. For example: document.getElementById("title").value = "";
document.getElementById("author").value = "";
document.getElementById("pages").value = "";
document.getElementById("check").checked = false;

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.

You can use any code as long as can explain the code when asked. In Piscine's interview, the interviewer may randomly pick a piece of code from your project and ask you to explain it.


$('#demo').collapse('hide');
});

function addBookToTable(title, author, pages, read) {
const table = document.getElementById("display").getElementsByTagName("tbody")[0];

const row = table.insertRow();

row.insertCell(0).textContent = title;
row.insertCell(1).textContent = author;
row.insertCell(2).textContent = pages;
row.insertCell(3).textContent = read ? "Yes" : "No";
}
Comment thread
cjyuan marked this conversation as resolved.
Outdated

const title = document.getElementById("title");
const author = document.getElementById("author");
const pages = document.getElementById("pages");
const check = document.getElementById("check");
function populateStorage(title, author, pages, check) {
let newBook = new Book(title, author, pages, check);
myLibrary.push(newBook);

saveLibrary();
}

//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 == ""
) {
alert("Please fill all fields!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
render();
}
}
function populateStorage(title, author, pages, check) {
let newBook = new Book(title, author, pages, check);

myLibrary.push(newBook);
saveLibrary();
};
Comment thread
cjyuan marked this conversation as resolved.
Outdated


function Book(title, author, pages, check) {
this.title = title;
Expand All @@ -51,16 +62,20 @@ function Book(title, author, pages, check) {
}

function render() {
let table = document.getElementById("display");
let rowsNumber = table.rows.length;
console.log("RENDER CALLED");

const table = document.querySelector("#display tbody");
table.innerHTML = "";

//let rowsNumber = table.rows.length;
//delete old table
for (let n = rowsNumber - 1; n > 0; n-- {
table.deleteRow(n);
}
//for (let n = rowsNumber - 1; n > 0; n--) {
// table.deleteRow(n);
//}
Comment thread
cjyuan marked this conversation as resolved.
Outdated
//insert updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
let row = table.insertRow(1);
let row = table.insertRow();
let titleCell = row.insertCell(0);
let authorCell = row.insertCell(1);
let pagesCell = row.insertCell(2);
Expand All @@ -72,32 +87,53 @@ 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;

changeBut.className = myLibrary[i].check
? "btn btn-success"
: "btn btn-secondary";

changeBut.innerText = myLibrary[i].check ? "Read" : "Unread";

wasReadCell.appendChild(changeBut);
let readStatus = "";
if (myLibrary[i].check == false) {
readStatus = "Yes";
} else {
readStatus = "No";
}
changeBut.innerText = readStatus;

//let readStatus = myLibrary[i].check ? "Yes" : "No";
//}

changeBut.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
saveLibrary();
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";
let delBut = document.createElement("button");

//delBut.id = i + 5;


delBut.className = "btn btn-danger btn-sm";
delBut.innerHTML = "Delete";
delBut.addEventListener("clicks", function () {
alert(`You've deleted title: ${myLibrary[i].title}`);

deleteCell.appendChild(delBut);

delBut.addEventListener("click", function () {
//alert(`You've deleted title: ${myLibrary[i].title}`);
myLibrary.splice(i, 1);

saveLibrary();

render();
Comment thread
cjyuan marked this conversation as resolved.
});
}
}
window.addEventListener("load", function () {
if (myLibrary.length === 0) {
populateStorage("Robison Crusoe", "Daniel Defoe", "252", true);
populateStorage( "The Old Man and the Sea", "Ernest Hemingway", "127", false);
saveLibrary();
}
Comment thread
cjyuan marked this conversation as resolved.
Outdated
}

render();
});
Loading