-
-
Notifications
You must be signed in to change notification settings - Fork 238
Sheffield | ITP-Jan-26 | Hayriye Saricicek | Sprint 2 | Book Library #457
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
Changes from all commits
ee161c1
e29edbc
388cbae
8397ef9
d24a199
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,103 +1,159 @@ | ||
| let myLibrary = []; | ||
| const myLibrary = []; | ||
|
|
||
| window.addEventListener("load", function (e) { | ||
| populateStorage(); | ||
| render(); | ||
| }); | ||
| function isValidAuthor(text) { | ||
| for (let char of text) { | ||
| const isLetter = | ||
| (char >= "a" && char <= "z") || (char >= "A" && char <= "Z"); | ||
|
cjyuan marked this conversation as resolved.
|
||
|
|
||
| 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(); | ||
| const isAllowedSymbol = ". ',&-".includes(char); | ||
|
|
||
| if (!isLetter && !isAllowedSymbol) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| function isValidTitle(text) { | ||
| for (let char of text) { | ||
| const isLetter = | ||
| (char >= "a" && char <= "z") || (char >= "A" && char <= "Z"); | ||
|
|
||
| const isNumber = char >= "0" && char <= "9"; | ||
|
|
||
| const isAllowedSymbol = " '.,!?&:-()\"".includes(char); | ||
|
|
||
| if (!isLetter && !isNumber && !isAllowedSymbol) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| function saveLibrary() { | ||
| localStorage.setItem("myLibrary", JSON.stringify(myLibrary)); | ||
| } | ||
|
|
||
| const title = document.getElementById("title"); | ||
| const author = document.getElementById("author"); | ||
| const pages = document.getElementById("pages"); | ||
| const check = document.getElementById("check"); | ||
| 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; | ||
|
|
||
| if (!isValidAuthor(author)) { | ||
| alert("Invalid author name"); | ||
| return; | ||
| } | ||
|
|
||
| if (!isValidTitle(title)) { | ||
| alert("Invalid book title"); | ||
| return; | ||
| } | ||
|
|
||
| const success = populateStorage(title, author, pages, read); | ||
|
|
||
| if (!success) return; // ❗ STOP if invalid | ||
|
|
||
| render(); | ||
| saveLibrary(); | ||
|
|
||
| this.reset(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems quite advanced. Are you familiar with the meaning of In Piscine, you may be asked something like this:
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = "";
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
| }); | ||
|
|
||
| //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!"); | ||
|
|
||
| function populateStorage(title, author, pages, check) { | ||
| if (!/^\d+$/.test(pages) || Number(pages) < 1 || Number(pages) > 5000) { | ||
| alert("Pages must be a whole number between 1 and 5000"); | ||
| return false; | ||
| } else { | ||
| let book = new Book(title.value, title.value, pages.value, check.checked); | ||
| library.push(book); | ||
| render(); | ||
| } | ||
|
|
||
| let newBook = new Book(title, author, pages, check); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you choose to keep the pages as string? It is not wrong, just unusual.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ive changed it now |
||
| myLibrary.push(newBook); | ||
| return true; | ||
| } | ||
|
|
||
| function Book(title, author, pages, check) { | ||
| this.title = title; | ||
| this.author = author; | ||
| this.pages = pages; | ||
| this.pages = Number(pages); | ||
| this.check = 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); | ||
| } | ||
| const tableBody = document.querySelector("#display tbody"); | ||
| tableBody.innerHTML = ""; | ||
|
|
||
| //insert updated row and cells | ||
| let length = myLibrary.length; | ||
| for (let i = 0; i < length; i++) { | ||
| let row = table.insertRow(1); | ||
| myLibrary.forEach((book, i) => { | ||
| 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.innerText = book.title; | ||
| authorCell.innerText = book.author; | ||
| pagesCell.innerText = book.pages; | ||
|
|
||
| //add and wait for action for read/unread button | ||
| let changeBut = document.createElement("button"); | ||
| changeBut.id = i; | ||
| changeBut.className = "btn btn-success"; | ||
|
|
||
| changeBut.className = book.check ? "btn btn-success" : "btn btn-secondary"; | ||
|
|
||
| changeBut.innerText = book.check ? "Read" : "Unread"; | ||
|
|
||
| wasReadCell.appendChild(changeBut); | ||
| let readStatus = ""; | ||
| if (myLibrary[i].check == false) { | ||
| readStatus = "Yes"; | ||
| } else { | ||
| readStatus = "No"; | ||
| } | ||
| changeBut.innerText = readStatus; | ||
|
|
||
| changeBut.addEventListener("click", function () { | ||
| myLibrary[i].check = !myLibrary[i].check; | ||
| book.check = !book.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.className = "btn btn-danger btn-sm"; | ||
| delBut.innerHTML = "Delete"; | ||
| delBut.addEventListener("clicks", function () { | ||
| alert(`You've deleted title: ${myLibrary[i].title}`); | ||
| myLibrary.splice(i, 1); | ||
|
|
||
| deleteCell.appendChild(delBut); | ||
|
|
||
| delBut.addEventListener("click", function () { | ||
| const deletedTitle = book.title; | ||
| myLibrary.splice(myLibrary.indexOf(book), 1); | ||
| saveLibrary(); | ||
| render(); | ||
|
cjyuan marked this conversation as resolved.
|
||
| alert(`You've deleted title: ${deletedTitle}`); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function loadDefaultBooks() { | ||
| myLibrary.push( | ||
| new Book("Robinson Crusoe", "Daniel Defoe", "252", true), | ||
| new Book("The Old Man and the Sea", "Ernest Hemingway", "127", false) | ||
| ); | ||
| } | ||
|
|
||
| window.onload = () => { | ||
| myLibrary.length = 0; | ||
|
|
||
| const saved = localStorage.getItem("myLibrary"); | ||
|
|
||
| if (saved) { | ||
| const parsed = JSON.parse(saved); | ||
| if (Array.isArray(parsed) && parsed.length > 0) { | ||
| myLibrary.push(...parsed); | ||
| } else { | ||
| loadDefaultBooks(); | ||
| } | ||
| } else { | ||
| loadDefaultBooks(); | ||
| } | ||
| render(); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.