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:
- Search for available rooms by date and type
- Create a reservation with guest information
- 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.
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'
Search for availability
Use the searchAvailableRooms() function to find available rooms.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
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.
Prepare guest data
Collect all required guest information: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
Create the reservation
Call createReservation() with the hotel, room number, guest data, and dates:const reservation = createReservation(
hotel,
301,
{
name: 'Ana García',
email: '[email protected]',
phone: '+34 612345678',
dni: '12345678A'
},
'2026-04-10',
'2026-04-15'
)
Validation and response
The function performs several validations before creating the reservation: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: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: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: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:
Room Availability
Date Validation
Guest Data
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'
}
Check-in and check-out dates must be valid:// Check-in cannot be in the past
// Check-out must be after check-in
// Minimum 1 night, maximum 14 nights
Date requirements:
- Check-in cannot be in the past
- Check-out must be after check-in
- Minimum stay: 1 night
- Maximum stay: 14 nights
All guest information must pass regex validation:// Email must match email regex
// Phone must match phone regex (e.g., +34 612345678)
// DNI must match DNI regex (e.g., 12345678A)
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