Skip to main content

Overview

This guide will walk you through the essential operations of JS Mini Hotel: creating a hotel, searching for available rooms, making a reservation, and checking in a guest.
All code examples in this guide are taken directly from the actual implementation in main.js.

Prerequisites

Ensure you have:
  • Node.js installed (for ES6 module support)
  • The JS Mini Hotel source files in your project
  • Basic understanding of JavaScript and ES6 modules

Step-by-Step Guide

1

Import Required Functions and Data

First, import the functions and data you’ll need:
import rooms from './data/rooms.json' with { type: 'json' }
import {
  createHotel,
  searchAvailableRooms,
  createReservation,
  addExtras,
  checkIn,
  checkOut
} from './main.js'
The rooms.json file contains your room inventory. You can customize this file to match your hotel’s actual rooms.
2

Create Your Hotel

Initialize your hotel with a name and room inventory:
const hotel = createHotel('Overlook Hotel', rooms)
console.log(hotel)
Expected Output:
{
  name: 'Overlook Hotel',
  rooms: [...], // Array of room objects
  reservations: [], // Active reservations
  history: [] // Completed/cancelled reservations
}
The hotel object is the central data structure that stores all hotel information.
3

Search for Available Rooms

Search for available rooms by date range and room type:
const availableRooms = searchAvailableRooms(
  hotel,
  '2026-03-19',
  '2026-03-19',
  'single'
)
console.log('Available: ', availableRooms)
Expected Output:
[
  {
    number: 101,
    type: 'single',
    pricePerNight: 50,
    status: 'available',
    features: ['wifi', 'tv']
  },
  // ... other available single rooms
]
The function automatically filters out rooms that have conflicting reservations for the specified dates.
4

Create a Reservation

Create a new reservation for a guest:
const reservation = createReservation(
  hotel,
  301,
  {
    name: 'Ana García',
    email: '[email protected]',
    phone: '+34 612345678',
    dni: '12345678A',
  },
  '2026-04-10',
  '2026-04-15',
)
console.log('Reservation: ', reservation)
Expected Output:
{
  id: 'RES-001',
  roomNumber: 301,
  guest: {
    name: 'Ana García',
    email: '[email protected]',
    phone: '+34 612345678',
    dni: '12345678A'
  },
  checkIn: '2026-04-10',
  checkOut: '2026-04-15',
  nights: 5,
  totalPrice: 750, // Calculated automatically
  status: 'confirmed',
  extras: []
}
The function validates:
  • Room exists and is available for those dates
  • Check-in is not in the past
  • Check-out is after check-in
  • Stay is between 1-14 nights
  • Guest data (email, phone, DNI) format
5

Add Extra Services (Optional)

Enhance the reservation with additional services:
const updatedReservation = addExtras(hotel, 'RES-001', [
  { name: 'Breakfast', price: 10, quantity: 5 },
  { name: 'Parking', price: 15, quantity: 5 },
])
console.log('Reserva con extras añadidos', updatedReservation)
Expected Output:
{
  id: 'RES-001',
  // ... other reservation fields
  totalPrice: 875, // 750 + (10*5) + (15*5)
  extras: [
    { name: 'Breakfast', price: 10, quantity: 5 },
    { name: 'Parking', price: 15, quantity: 5 }
  ]
}
The total price is automatically recalculated when extras are added.
6

Check In the Guest

When the guest arrives, perform the check-in:
const checkInResult = checkIn(hotel, 'RES-001')
console.log(checkInResult)
Expected Output:
El check-in de la reserva RES-001 se hizo correctamente en la habitacion no. 301
What happens during check-in:
  • Reservation status changes to "checked-in"
  • Room status changes to "occupied"
  • Check-in date is updated to today’s date
Check-in validates that the reservation exists, is confirmed, the check-in date is valid, and the room is available.

Complete Working Example

Here’s a complete example that ties everything together:
import rooms from './data/rooms.json' with { type: 'json' }
import {
  createHotel,
  searchAvailableRooms,
  createReservation,
  addExtras,
  checkIn,
} from './main.js'

// 1. Create hotel
const hotel = createHotel('Overlook Hotel', rooms)

// 2. Search for available rooms
const availableRooms = searchAvailableRooms(
  hotel,
  '2026-04-10',
  '2026-04-15',
  'suite'
)
console.log(`Found ${availableRooms.length} available suites`)

// 3. Create a reservation
const reservation = createReservation(
  hotel,
  301,
  {
    name: 'Ana García',
    email: '[email protected]',
    phone: '+34 612345678',
    dni: '12345678A',
  },
  '2026-04-10',
  '2026-04-15',
)
console.log(`Reservation created: ${reservation.id}`)
console.log(`Total: €${reservation.totalPrice}`)

// 4. Add extras
addExtras(hotel, reservation.id, [
  { name: 'Breakfast', price: 10, quantity: 5 },
  { name: 'Parking', price: 15, quantity: 5 },
])
console.log(`New total: €${reservation.totalPrice}`)

// 5. Check in (when guest arrives)
const checkInMessage = checkIn(hotel, reservation.id)
console.log(checkInMessage)

Additional Operations

Check Out and Generate Invoice

const invoice = checkOut(hotel, 'RES-001')
console.log('Invoice', invoice)
This will:
  • Change reservation status to "checked-out"
  • Change room status to "available"
  • Move reservation from reservations to history
  • Return a detailed invoice

Cancel a Reservation

const cancellation = cancelReservation(hotel, 'RES-001')
console.log('Reserva cancelada', cancellation)
Output:
Cancelación realizada con una penalización de XX€
The penalty is calculated based on how far in advance the cancellation is made.

Generate Reports

const occupancyReport = getOccupancyReport(hotel, '2026-03-22')
console.log(occupancyReport)

Data Structures Reference

{
  name: "Hotel Keepcoding",
  rooms: [...],        // Array of room objects
  reservations: [...], // Active reservations
  history: [...]       // Completed/cancelled reservations
}
{
  number: 101,
  type: "single",              // "single", "double", "suite"
  pricePerNight: 50,
  status: "available",         // "available", "occupied", "maintenance"
  features: ["wifi", "tv", "minibar"]
}
{
  id: "RES-001",
  roomNumber: 101,
  guest: {
    name: "Ana García",
    email: "[email protected]",
    phone: "+34 612345678",
    dni: "12345678A"
  },
  checkIn: "2026-01-20",
  checkOut: "2026-01-25",
  nights: 5,
  totalPrice: 250,
  status: "confirmed",        // "confirmed", "checked-in", "checked-out", "cancelled"
  extras: [
    { name: "Breakfast", price: 10, quantity: 5 },
    { name: "Parking", price: 15, quantity: 5 }
  ]
}

Next Steps

API Reference

Explore detailed documentation for all functions

Validation Rules

Learn about data validation and business rules

Managing Extras

Learn how to add extra services to reservations

Generating Reports

See how to generate occupancy and revenue reports

Build docs developers (and LLMs) love