Skip to main content
This guide walks you through the complete process of searching for available rooms and creating reservations in the JS Mini Hotel system.

Overview

The reservation workflow consists of three main steps:
  1. Search for available rooms by date and type
  2. Create a reservation with guest information
  3. Handle validation errors and confirmations

Searching for Available Rooms

Before creating a reservation, you need to find available rooms for your desired dates and room type.
1

Define search parameters

Specify the check-in date, check-out date, and room type (optional).
const checkIn = '2026-03-19'
const checkOut = '2026-03-19'
const roomType = 'single' // 'single', 'double', or 'suite'
2

Search for availability

Use the searchAvailableRooms() function to find available rooms.
main.js:104-113
const availableRooms = searchAvailableRooms(
  hotel,
  '2026-03-19',
  '2026-03-19',
  'single'
)
This function:
  • Filters rooms by the specified type
  • Checks which rooms are reserved for the date range
  • Returns only rooms that are not reserved
3

Review available rooms

The function returns an array of room objects:
[
  {
    number: 101,
    type: 'single',
    pricePerNight: 50,
    status: 'available',
    features: ['wifi', 'tv']
  },
  // ... more rooms
]
A room is considered unavailable if there’s a confirmed reservation where the dates overlap with your search range.

Creating a Reservation

Once you’ve identified an available room, you can create a reservation.
1

Prepare guest data

Collect all required guest information:
main.js:51-56
const guestData = {
  name: 'Ana García',
  email: '[email protected]',
  phone: '+34 612345678',
  dni: '12345678A'
}
All guest fields are validated:
  • Email: Must match email regex pattern
  • Phone: Must match phone regex pattern
  • DNI: Must match DNI regex pattern
2

Create the reservation

Call createReservation() with the hotel, room number, guest data, and dates:
main.js:46-60
const reservation = createReservation(
  hotel,
  301,
  {
    name: 'Ana García',
    email: '[email protected]',
    phone: '+34 612345678',
    dni: '12345678A'
  },
  '2026-04-10',
  '2026-04-15'
)
3

Validation and response

The function performs several validations before creating the reservation:
main.js:120-149
function createReservation(hotel, roomNumber, guestData, checkIn, checkOut) {
  // Validate the reservation
  const isValidReservation = validateReservation(
    hotel,
    roomNumber,
    guestData,
    checkIn,
    checkOut,
  )
  if (isValidReservation.status === false) {
    console.log(isValidReservation.message)
    return
  }
  
  const room = getRoomByNumber(hotel, roomNumber)
  
  // Create reservation object
  const reservation = {
    id: createUniqueReservationId(),
    roomNumber,
    guest: guestData,
    checkIn: checkIn.toString(),
    checkOut: checkOut.toString(),
    nights: getNumberOfNights(checkIn, checkOut),
    totalPrice: getTotalPrice(room, checkIn, checkOut),
    status: 'confirmed',
    extras: []
  }
  
  hotel.reservations.push(reservation)
  return reservation
}

Automatic Calculations

The system automatically calculates key reservation details:
The system calculates the number of nights based on check-in and check-out dates:
utils.js:49-54
export function getNumberOfNights(checkInString, checkOutString) {
  const checkIn = Temporal.PlainDate.from(checkInString)
  const checkOut = Temporal.PlainDate.from(checkOutString)

  return checkOut.dayOfYear - checkIn.dayOfYear
}
Example: Check-in on April 10, check-out on April 15 = 5 nights
The total price is calculated by multiplying the room’s nightly rate by the number of nights:
utils.js:88-94
export function getTotalPrice(room, checkIn, checkOut) {
  const nights = getNumberOfNights(
    Temporal.PlainDate.from(checkIn),
    Temporal.PlainDate.from(checkOut)
  )
  return room.pricePerNight * nights
}
Price by room type:
  • Single: €50/night
  • Double: €80/night
  • Suite: €150/night
Each reservation receives a unique ID generated from a timestamp and random number:
utils.js:4-13
export function createUniqueReservationId() {
  const timestamp = Temporal.Now.instant()
    .toString()
    .replaceAll('-', '')
    .replaceAll(':', '')
    .replaceAll('.', '')

  const random = Math.floor(Math.random() * 100)
  return `RES-${timestamp}-${random}`
}
Example: RES-20260410T143027123456789Z-42

Handling Validation Errors

The validateReservation() function checks multiple conditions before allowing a reservation to be created:
The room must exist and be available for the specified dates:
// Error if room is already reserved
{
  status: false,
  message: 'Room is not available for these dates'
}

Complete Example

Here’s a complete workflow from searching to creating a reservation:
// 1. Search for available rooms
const availableRooms = searchAvailableRooms(
  hotel,
  '2026-04-10',
  '2026-04-15',
  'suite'
)

console.log(`Found ${availableRooms.length} available suites`)

// 2. Create reservation with the first available room
if (availableRooms.length > 0) {
  const reservation = createReservation(
    hotel,
    availableRooms[0].number, // Room 301
    {
      name: 'Ana García',
      email: '[email protected]',
      phone: '+34 612345678',
      dni: '12345678A'
    },
    '2026-04-10',
    '2026-04-15'
  )

  console.log('Reservation created:', reservation)
}
Always search for available rooms before attempting to create a reservation to ensure the room is available for your dates.

Next Steps

Managing Extras

Learn how to add breakfast, parking, and other services to reservations

Cancellation Policy

Understand cancellation fees and how to cancel reservations

Build docs developers (and LLMs) love