• in
InterCourses
CoursesBlogs
0
← Introduction to JavaScript
○What is JavaScript?○Your First JavaScript Code○Data Types○Type Checking○Inspect Values in the Console○Inspect the Queue Report○Trace a Faulty Calculation○Repair the Score Calculation✦Module Quiz
○Declaring Variables○let and const in Practice○Operators○Type Conversion○Group and Convert Expense Inputs○Calculate the Event Expense Total○Track a Changing Points Tally○Adjust a Ticket Price✦Module Quiz
○Conditionals○Demo: Combined Conditions Before FizzBuzz○Challenge: FizzBuzz Announcer○Switch Statement○Demo: Switch a Venue Sign○Challenge: Power Loadout○For Loops○While Loop○Break and Continue○Demo: Stop a Ticket Scan○Challenge: Number Guess Referee○Demo: Rainfall Routing Walkthrough○Exercise: Score Batch Report✦Module Quiz
○Defining Functions○Arrow Functions○Scope○Closures○Project - Calculator○Function Expressions○Demo: Ticket Pricing Walkthrough○Exercise: Shipping Labels with Defaults○Demo: Seat Ledger Closure Walkthrough○Exercise: Private Budget Ledger✦Module Quiz
○Arrays○Demo: Student Scoreboard○Challenge: Student Course Scoreboard○Array Mutation Methods○Array Slice and Concat○Array Iteration Methods○Multidimensional Arrays○Destructuring○Array Reverse and Sort○Array Fill○Array Find and Includes○Array Flat and FlatMap○Array Reduce, Some, and Every○Demo: Charging Rack Restock Report○Exercise: Pantry Restock Plan✦Module Quiz
○Object Key-Value Pairs○Student Course Record○Object Methods and Destructuring○Map and Set Collections○Playlist Lookup○Number and BigInt Boundaries○Ticket Ledger○JSON○Math and Date○Date Basics○Demo: Supply Bin Audit○Exercise: Volunteer Shift Audit○Demo: Workshop Attendance Feed○Exercise: Parcel Scan Feed✦Module Quiz
○String Methods○Template Literals○Regular Expressions○Demo: Build a Reminder with Template Literals○Exercise: Format Pantry Pickup Messages○Demo: Validate a Time Slot with Regex○Exercise: Validate Storage Shelf Labels✦Module Quiz
○Higher-Order Functions○Callbacks○map, filter, and reduce○Demo: Student Register with map○Challenge: Student Register○Recursion○Project - Data Transform Pipeline○Demo: Handle an Error-First Callback○Exercise: Report Seat Reservation Results○Demo: Summarize Completed Bike Rides○Exercise: Summarize Delivered Parcels✦Module Quiz
○Demo: A notebook object○OOP Introduction○Demo: Independent workshop passes○Classes○Demo: An inherited notice○Inheritance○Demo: A private seat counter○Encapsulation○Demo: Equipment desk checkout●Project: Library Checkout✦Module Quiz
○Demo: Callback Timing○Exercise: Asynchronous JavaScript○Demo: Promise Rehearsal○Exercise: Promises and Async Await○Demo: Offline API Coordination○Exercise: Async API Simulation○Demo: Partial Results Bulletin○Project: Resilient Awards Feed○Demo: Retry a Local Delivery○Project: Reliable Score Delivery✦Module Quiz

Project: Library Checkout

The equipment desk demo showed guarded checkout behavior inherited by a specialized class. The private field still belongs to the base class and cannot be read directly by the child. Now build a library checkout system from scratch in script.js. Books record authors and describe their own availability. All results go to the Console, not a page element.

LibraryItem owns the private status; Book inherits checkout and return behavior. The checkpoints make it possible to inspect transitions after each operation. Do not expose the private field directly or write book.#checkedOut outside the class.

Worked example

A different private-state class exposes behavior without exposing its field:

javascript
class Lamp {
  #on = false;
  switchOn() { this.#on = true; }
  get isOn() { return this.#on; }
}
const lamp = new Lamp();
lamp.switchOn();
console.log(lamp.isOn); // true

Your Task

  1. Store the title in the LibraryItem constructor. The starter checks its title.
  2. Implement the available getter using the initial private #checkedOut state. The starter checks a new item.
  3. Store the author in Book while inheriting from LibraryItem. The starter checks the author.
  4. Implement checkout() to mark an available book on loan and return true. The starter checks the result and availability.
  5. Make a second checkout return false without changing availability. The starter checks the repeated call.
  6. Implement returnItem() to restore availability and return true. The starter checks the result.
  7. Make a repeated return yield false. The starter checks the repeated call.
  8. Implement Book.describe() to return River Atlas by N. Vale — available after the return. The starter logs it.

Section complete

You can model shared behavior and protect state while still exposing useful operations. Take the quiz to check where each responsibility belongs.

Hint 1
1 / 3
HINT 1

Call super(title) before assigning the book author.

HINT 2

Keep the loan state in #checkedOut; change it only inside methods.

HINT 3

Return false for an invalid checkout or return without changing state.

Take the Quiz →
Loading editor…
Press Run to execute your code.
READY
intercourses
javascript