JS Lab: Library

In this lab, similarly to the Python lab, you’ll be working on a simple “database” for a library to understand CRUD operations in relation to representing redundant, similar data under one structure – an abstraction.

For JavaScript, you’ll have to open the web console from Developer Tools (ctrl + shift + p -> Developer: Toggle developer tools).

%%javascript
// Our "database" is an array of objects, each representing a book in a library.
// Arrays allow us to store multiple records in a single variable, making it easy to manage collections of data.
let library = [
    { title: "1984", author: "George Orwell", checkedOut: false },
    { title: "To Kill a Mockingbird", author: "Harper Lee", checkedOut: true },
    { title: "The Great Gatsby", author: "F. Scott Fitzgerald", checkedOut: false }
];

// Arrays provide order and allow us to add, remove, or update records efficiently.
// Each element in the array is an object, which abstracts the details of each book.

// Function to display all books
function displayLibrary(lib) {
    console.log("All books in the library:");
    lib.forEach((book, i) => {
        console.log(`Index ${i}:`, book);
    });
}

// Function to add a new book (students: implement prompt and push logic)
function addBook(lib) {
    let title = prompt("Enter book title:");
    let author = prompt("Enter book author:");
    let checkOutInput = prompt("Is the book checked out? (yes/no):");
    let checkedOut = checkOutInput.toLowerCase() === "yes";

    let newBook = {title: title, author: author, checkedOut: checkedOut};
    lib.push(newBook);
    console.log("Book added successfully!")
}

// Function to find a book by title (students: implement search logic)
function findBook(lib, searchTitle) {
    for (let book of lib){
       if (book.title === searchTitle) {
            console.log("Book found:", book);
            return;
        }
    }
    console.log("Book not found.");
}

// Function to update a book's checkedOut status (students: implement update logic)
function updateBook(lib, searchTitle) {
  for (let book of lib) {
        if (book.title === searchTitle) {
            console.log("Current book info:", book);
            let checkedOutInput = prompt("Is the book checked out? (yes/no):");
            book.checkedOut = checkedOutInput.toLowerCase() === "yes";
            console.log("Book updated successfully!");
            return;
        }
    }
    console.log("Book not found.");
}

// Function to delete a book (students: implement delete logic)
function deleteBook(lib, searchTitle) {
    for (let i = 0, len = lib.length; i < len; i++) {
        if (lib[i].title === searchTitle){
            lib.splice(i,1);
            console.log("Book deleted successfully!");
            return;
        }
    }
    console.log("Book not found.");
}

// Example usage
displayLibrary(library);
// Students: Uncomment and complete the following as you implement
// addBook(library);
// findBook(library, "1984");
// updateBook(library, "To Kill a Mockingbird");
// deleteBook(library, "The Great Gatsby");
<IPython.core.display.Javascript object>