-
-
Notifications
You must be signed in to change notification settings - Fork 161
Manchester | 25-ITP-Sep | Mahtem T. Mengstu | Sprint 2 | Book_library #353
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 2 commits
9198e90
7fe03c6
eda7bcc
cb7521d
ee1ce99
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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| // Use IntelliSense to learn about possible attributes. | ||
| // Hover to view descriptions of existing attributes. | ||
| // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
| "version": "0.2.0", | ||
| "configurations": [ | ||
| { | ||
| "type": "chrome", | ||
| "request": "launch", | ||
| "name": "Launch Chrome against localhost", | ||
| "url": "http://localhost:8080", | ||
| "webRoot": "${workspaceFolder}" | ||
| } | ||
| ] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ window.addEventListener("load", function (e) { | |
|
|
||
| function populateStorage() { | ||
| if (myLibrary.length == 0) { | ||
| let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); | ||
| let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true); | ||
| let book2 = new Book( | ||
| "The Old Man and the Sea", | ||
| "Ernest Hemingway", | ||
|
|
@@ -28,21 +28,56 @@ 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() { | ||
| // Letters + spaces only (authors) | ||
| const lettersOnly = /^[A-Za-z\s]+$/; | ||
|
|
||
| // Title: letters + numbers + spaces (no special characters) | ||
| const titleAllowed = /^[A-Za-z0-9\s]+$/; | ||
|
||
|
|
||
| // Empty field check | ||
| if ( | ||
| title.value == null || | ||
| title.value == "" || | ||
| pages.value == null || | ||
| pages.value == "" | ||
| title.value.trim() === "" || | ||
| author.value.trim() === "" || | ||
| pages.value.trim() === "" | ||
| ) { | ||
| alert("Please fill all fields!"); | ||
| return false; | ||
| } else { | ||
| let book = new Book(title.value, title.value, pages.value, check.checked); | ||
| library.push(book); | ||
| render(); | ||
| } | ||
|
|
||
| // Title validation | ||
| if (!titleAllowed.test(title.value)) { | ||
| alert("Title must contain only letters, numbers, and spaces!"); | ||
| return false; | ||
| } | ||
|
|
||
| // Author validation | ||
| if (!lettersOnly.test(author.value)) { | ||
| alert("Author name must contain only letters!"); | ||
| return false; | ||
| } | ||
|
|
||
| // Pages >= 1 | ||
| let pagesNum = Number(pages.value); | ||
|
|
||
| if (isNaN(pagesNum) || pagesNum < 1) { | ||
| alert("Pages must be a number greater than or equal to 1!"); | ||
| return false; | ||
| } | ||
|
|
||
| // Create and save the book | ||
| let book = new Book(title.value, author.value, pages.value, check.checked); | ||
|
||
| myLibrary.push(book); | ||
|
|
||
| // Clear inputs (optional) | ||
| title.value = ""; | ||
| author.value = ""; | ||
| pages.value = ""; | ||
| check.checked = false; | ||
|
|
||
| render(); | ||
| } | ||
|
|
||
|
|
||
| function Book(title, author, pages, check) { | ||
| this.title = title; | ||
| this.author = author; | ||
|
|
@@ -53,8 +88,8 @@ 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-- { | ||
| // delete old table rows (keep header row 0) | ||
| for (let n = rowsNumber - 1; n > 0; n--) { | ||
| table.deleteRow(n); | ||
| } | ||
| //insert updated row and cells | ||
|
|
@@ -72,11 +107,12 @@ function render() { | |
|
|
||
| //add and wait for action for read/unread button | ||
| let changeBut = document.createElement("button"); | ||
| changeBut.id = i; | ||
| changeBut.id = "read-btn-" + i; | ||
| changeBut.className = "btn btn-success"; | ||
| wasReadCell.appendChild(changeBut); | ||
| let readStatus = ""; | ||
| if (myLibrary[i].check == false) { | ||
| // show "Yes" when check is true (read), otherwise "No" | ||
| if (myLibrary[i].check === true) { | ||
| readStatus = "Yes"; | ||
| } else { | ||
| readStatus = "No"; | ||
|
|
@@ -89,15 +125,19 @@ function render() { | |
| }); | ||
|
|
||
| //add delete button to every row and render again | ||
| let delButton = document.createElement("button"); | ||
| delBut.id = i + 5; | ||
| deleteCell.appendChild(delBut); | ||
| let delBut = document.createElement("button"); | ||
| delBut.id = "del-btn-" + i; | ||
| delBut.className = "btn btn-warning"; | ||
| delBut.innerHTML = "Delete"; | ||
| delBut.addEventListener("clicks", function () { | ||
| deleteCell.appendChild(delBut); | ||
|
|
||
| delBut.addEventListener("click", function () { | ||
| alert(`You've deleted title: ${myLibrary[i].title}`); | ||
| myLibrary.splice(i, 1); | ||
| render(); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // Book Library bugs fixed and Book Library working normal. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON != JS. A JSON string containing JS-style comments is considered invalid.
Better to use
.gitignoreto exclude non-essential configuration files from being added to a repo.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I will see what it really is. It happened accidentally.