Library | JavaScript | Full Stack JavaScript (Getting Hired, NodeJS and MongoDB, React, HTML and CSS Advanced, JavaScript, HTML and CSS Intermediate) | 01/12 (03/29) | The Odin Project | 2024
-
If you haven’t already, set up your project with skeleton HTML/CSS and JS files.
-
Write a constructor for making “Book” objects. Your book objects should have the book’s
title,author, the number ofpages, and whether or not you havereadthe book.- Put a function into the constructor that can report the book info like so:
theHobbit.info(); // "The Hobbit by J.R.R. Tolkien, 295 pages, not read yet"- Note: It is almost always best to
returnthings rather than puttingconsole.log()directly into the function. In this case, return theinfostring and log it after the function has been called:
console.log(theHobbit.info()); -
All of your book objects are going to be stored in an array, so add a function to the script (not the constructor) that can take user’s input and store the new book objects into an array. Your code should look something like this:
const myLibrary = [];
function Book() {
// the constructor...
}
function addBookToLibrary() {
// do stuff here
}
-
Write a function that loops through the array and displays each book on the page. You can display them in some sort of table, or each on their own “card”. It might help for now to manually add a few books to your array so you can see the display.
-
Add a “NEW BOOK” button that brings up a form allowing users to input the details for the new book: author, title, number of pages, whether it’s been read and anything else you might want.
- How you decide to display this form is up to you. For example, you may wish to have a form show in a sidebar or you may wish to explore dialogs and modals - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog using the
<dialog>tag. - However you do this, you will most likely encounter an issue where submitting your form will not do what you expect it to do. That’s because the
submitinput tries to send the data to a server by default. This is whereevent.preventDefault();will come in handy. Check out the documentation forevent.preventDefault- https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault and see how you can solve this issue!
- How you decide to display this form is up to you. For example, you may wish to have a form show in a sidebar or you may wish to explore dialogs and modals - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog using the
-
Add a button on each book’s display to remove the book from the library.
- You will need to associate your DOM elements with the actual book objects in some way. One easy solution is giving them a data-attribute that corresponds to the index of the library array.
-
Add a button on each book’s display to change its
readstatus.- To facilitate this you will want to create the function that toggles a book’s
readstatus on yourBookprototype instance.
- To facilitate this you will want to create the function that toggles a book’s
-
You’re not required to add any type of storage right now to save the information between page reloads. You will have the option to come back to this project later on in the course.