diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..1a2b5b95 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,96 +1,67 @@ - - - - - - - - - - + - -
-

Library

-

Add books to your virtual library

-
+ + + + + My Virtual Library + + + + + + - + +
+

Library

+

Add books to your virtual library

+
-
-
- - - - - - - - -
-
+ - - - - - - - - - - - - - - - - - - - -
TitleAuthorNumber of PagesRead
+
+
+ + + + + + + + +
+
- - - + + + + + + + + + + + + +
TitleAuthorNumber of PagesReadDelete
+ + + + + \ No newline at end of file diff --git a/debugging/book-library/readme.md b/debugging/book-library/readme.md index 3abe8c13..025dde65 100644 --- a/debugging/book-library/readme.md +++ b/debugging/book-library/readme.md @@ -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! diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..8ac6e59f 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -2,16 +2,21 @@ let myLibrary = []; window.addEventListener("load", function (e) { populateStorage(); - render(); + const form = document.querySelector('form'); + form?.addEventListener("submit", e => { + e.preventDefault(); + }); + const addBookBtn = document.getElementById("add-book-btn"); + addBookBtn?.addEventListener("click", addBook); }); function populateStorage() { - if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); + if (myLibrary.length === 0) { + let book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true); let book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", - "127", + 127, true ); myLibrary.push(book1); @@ -20,27 +25,30 @@ 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 == "" - ) { - 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 addBook() { + const titleInput = document.getElementById("title"); + const authorInput = document.getElementById("author"); + const pagesInput = document.getElementById("pages"); + const checkInput = document.getElementById("check"); + const titleValue = titleInput.value.trim(); + const authorValue = authorInput.value.trim(); + const pagesValue = Number(pagesInput.value); + if (!titleValue || !authorValue) { + alert("Please fill in all the fields!"); + return; + } + if (pagesValue < 1 || !Number.isInteger(pagesValue)) { + alert("Pages must be a positive integer!"); + return; } + let book = new Book(titleValue, authorValue, pagesValue, checkInput.checked); + myLibrary.push(book); + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + checkInput.checked = false; + alert(`You've added ${book.title} to your library.`); + render(); } function Book(title, author, pages, check) { @@ -52,52 +60,43 @@ 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); - } - //insert updated row and cells + const tbody = table.querySelector('tbody'); + if (!tbody) return; + tbody.innerHTML = ''; let length = myLibrary.length; for (let i = 0; i < length; i++) { - let row = table.insertRow(1); + let row = tbody.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; - - //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 readStatus = ""; - if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; + titleCell.textContent = myLibrary[i].title; + authorCell.textContent = myLibrary[i].author; + pagesCell.textContent = myLibrary[i].pages; + let toggleReadButton = document.createElement("button"); + toggleReadButton.type = "button"; + wasReadCell.appendChild(toggleReadButton); + let readStatus = myLibrary[i].check ? "Yes" : "No"; + toggleReadButton.textContent = readStatus; + toggleReadButton.className = 'btn ' + (myLibrary[i].check ? 'btn-success' : 'btn-danger'); - changeBut.addEventListener("click", function () { + toggleReadButton.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"); + deleteButton.type = "button"; + deleteCell.appendChild(deleteButton); + deleteButton.className = "btn btn-danger"; + deleteButton.textContent = "Delete"; + deleteButton.addEventListener("click", function () { + const deletedTitle = myLibrary[i].title; myLibrary.splice(i, 1); render(); + alert(`You've deleted ${deletedTitle} from your library.`); }); } } diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..f7f8035a 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,19 +1,45 @@ +.collapse.show{ + display: flex; +} + .form-group { width: 400px; height: 300px; - align-self: left; + align-self: flex-start; padding-left: 20px; } -.btn { - display: block; +.table .btn, +.form-group .btn { + color: white; } .form-check-label { padding-left: 20px; - margin: 5px 0px 5px 0px; + padding-top: 10px; + margin: 5px ; } -button.btn-info { +button[data-toggle="collapse"] { margin: 20px; } + +#add-book-btn { + display: block; + margin-top: 10px; +} + +.btn-primary { + background-color: blue; + border-color: blue; +} + +.btn-success { + background-color: green; + border-color: green; +} + +.btn-danger { + background-color: rgb(199, 0, 0); + border-color: rgb(199, 0, 0); +} \ No newline at end of file diff --git a/debugging/code-reading/readme.md b/debugging/code-reading/readme.md index 4090c14c..d971ba56 100644 --- a/debugging/code-reading/readme.md +++ b/debugging/code-reading/readme.md @@ -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: @@ -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 + + ## Question 3 Take a look at the following code: @@ -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 \ No newline at end of file