Skip to main content

createHotel

Creates and initializes a new hotel object with rooms, reservations, and history.

Function Signature

function createHotel(name, rooms)

Parameters

name
string
required
The name of the hotel
rooms
array
required
Array of room objects. Each room should contain:
  • number (number): Room number
  • type (string): Room type (‘single’, ‘double’, or ‘suite’)
  • pricePerNight (number): Price per night for the room
  • status (string): Current room status (‘available’, ‘occupied’, etc.)

Return Value

hotel
object
The initialized hotel object

Example

import rooms from './data/rooms.json' with { type: 'json' }

const hotel = createHotel('Overlook Hotel', rooms)
console.log(hotel)

Expected Output

{
  name: 'Overlook Hotel',
  rooms: [
    { number: 101, type: 'single', pricePerNight: 50, status: 'available' },
    { number: 201, type: 'double', pricePerNight: 80, status: 'available' },
    // ... more rooms
  ],
  reservations: [],
  history: []
}
The hotel object is initialized with mock data for reservations and history from JSON imports.

searchAvailableRooms

Searches for available rooms of a specific type within a given date range.

Function Signature

function searchAvailableRooms(hotel, checkIn, checkOut, roomType)

Parameters

hotel
object
required
The hotel object to search within
checkIn
string
required
Check-in date in ISO format (e.g., ‘2026-03-19’)
checkOut
string
required
Check-out date in ISO format (e.g., ‘2026-03-22’)
roomType
string
required
Type of room to search for. Valid values:
  • 'single' - Single room
  • 'double' - Double room
  • 'suite' - Suite room

How It Works

The function performs three filtering steps:
  1. Filter by Type: Filters all hotel rooms to match the requested roomType
  2. Get Reserved Rooms: Calls getReservedRoomsByDate() to get a Set of room numbers that are reserved (with status ‘confirmed’) for the specified date range
  3. Filter Available: Returns only rooms of the requested type that are NOT in the reserved rooms set

Return Value

availableRooms
array
Array of available room objects matching the criteria

Example

const availableRooms = searchAvailableRooms(
  hotel, 
  '2026-03-19', 
  '2026-03-22', 
  'single'
)

console.log('Available:', availableRooms)

Expected Output

[
  { number: 101, type: 'single', pricePerNight: 50, status: 'available' },
  { number: 102, type: 'single', pricePerNight: 50, status: 'available' },
  { number: 105, type: 'single', pricePerNight: 55, status: 'available' }
]
Only rooms with status ‘confirmed’ in reservations are considered as reserved. Cancelled or checked-out reservations don’t affect availability.
This function uses the following helper functions from utils.js:
  • getReservedRoomsByDate() - Returns a Set of room numbers reserved for the date range (main.js:109)

Build docs developers (and LLMs) love