3.2 Data Abstractions JS Hack
Categories: CSP Big Idea 3Hack(s) for intro to data abstractions in JS.
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
let library = [
{ title: "1984", author: "George Orwell", checkedOut: false },
{ title: "Fahrenheit 451", author: "Ray Bradbury", 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, title, author, checkedOut) {
// If parameters are not provided, fall back to prompt (keeps interactive behavior)
if (typeof title === 'undefined') {
title = prompt('Title:');
author = prompt('Author:');
const co = prompt('Checked out? (true/false)');
checkedOut = co === 'true';
}
const book = { title: title, author: author, checkedOut: !!checkedOut };
lib.push(book);
console.log('Added book:', book);
return lib.length - 1; // return index
}
// Function to find a book by title (students: implement search logic)
function findBook(lib, searchTitle) {
const idx = lib.findIndex(b => b.title === searchTitle);
if (idx >= 0) {
console.log(`Found book at index ${idx}:`, lib[idx]);
return idx;
}
console.log(`No book found with title: ${searchTitle}`);
return -1;
}
function updateBook(lib, searchTitle, updates) {
const idx = lib.findIndex(b => b.title === searchTitle);
if (idx === -1) {
console.log(`No book found with title: ${searchTitle} (nothing updated)`);
return false;
}
const book = lib[idx];
// updates is an object like { checkedOut: true } or { author: 'New' }
Object.assign(book, updates || {});
lib[idx] = book;
console.log(`Updated book at index ${idx}:`, book);
return true;
}
// Function to delete a book (students: implement delete logic)
function deleteBook(lib, searchTitle) {
const idx = lib.findIndex(b => b.title === searchTitle);
if (idx === -1) {
console.log(`No book found with title: ${searchTitle} (nothing deleted)`);
return false;
}
const removed = lib.splice(idx, 1)[0];
console.log(`Deleted book at index ${idx}:`, removed);
return true;
}
// Example usage (demo, non-blocking)
displayLibrary(library);
// Demo: add a book without prompts
addBook(library, "Diana", "Diana Author", false);
// Demo: find a book
findBook(library, "Diana");
updateBook(library, "Diana", { checkedOut: true });
// Demo: delete a book
deleteBook(library, "The Great Gatsby");
// Final state
console.log('\n-- Final library state --');
displayLibrary(library);
<IPython.core.display.Javascript object>